Comments (6)
A little hack ... but I still think that represent the same object with different classes is a bad idea
require 'geometry'
module Geometry
class Line
def interception(line)
if line.vertical? && self.vertial? then
return nil
end
if line.vertical? then
m = self.slope
c = self.intercept
x = self.first.x
y = m * x + c
return Point[x,y]
end
if self.vertical? then
m = line.slope
c = line.intercept
x = line.first.x
y = m * x + c
return Point[x,y]
end
m1 = self.slope
m2 = line.slope
c1 = self.intercept
c2 = line.intercept
return nil if m1 == m2
x = (c2 - c1) / (m1 - m2)
y = m1 * x + c1
return Point[x,y]
end
end
class TwoPointLine
def intercept(axis=:y)
y1 = first.y ; y2 = last.y
x1 = first.x ; x2 = last.x
case axis
when :x
return - y1 * (x2 - x1 ) / ( y2 - y1) + x1
when :y
return - x1 * (y2 - y1 ) / ( x2 - x1) + y1
end
end
def vertical?
x1 = first.x ; x2 = last.x
x1 == x2
end
def horizontal?
y1 = first.y ; y2 = last.y
y1 == y2
end
end
#test
if __FILE__ == $0 then
line1 = Line[[1,1],[3,3]]
line2 = Line[[1,3],[3,1]]
#it should be Point[2,2] in both cases
p line1.interception(line2)
p line2.interception(line1)
end
end
from geometry.
Thanks for looking into this. Line is implemented as a class cluster to avoid the conversion losses inherent in forcing all lines to be represented in the same way. Although, the current implementation does need a little cleaning.
I'm confused about what you're trying to implement here. Is this about adding an intersection method, or an intercept method?
from geometry.
I need the intercept in order to implement my intersection method. You can ignore it if you don't like it but it would be nice to have one. but intercept method should be available for all the instances because is a property of the line, no matter how it has been generated.
from geometry.
I've added an intercept method to all of the Line subclasses. Can you create a pull request for the intersection method?
from geometry.
Should this method return a point or an array of points? If you are thinking in extend it to interception with polygons in the future it should return an array.
BtW. Can I close this issue? I created it for the intercept method and it's done now.
from geometry.
I'm tempted to say it should return an Array, but Edge#intersection already returns a Point. Edge is more specialized (it was made for Polygon), so maybe that's ok. Either way, we can discuss this in another issue.
from geometry.
Related Issues (20)
- Add a Rotation::Identity class
- Circle#minmax does the wrong thing when center is a PointZero HOT 1
- Slope of a Line defined by 2 points HOT 1
- feature: y = f(x=3) HOT 3
- Add a Plane object
- Add Polyline#close and Polyline#close! HOT 1
- Add Polyline#reverse and Polyline#reverse!
- Add Polyline#spokes
- Size + Array and Array + Size should do the right thing
- Splinegon
- Add PointOne and SizeOne HOT 1
- Implement Wikipedia's list of geometric shapes
- Add Rectangle#closed? HOT 1
- Figure out why overriding #closed? in Polygon breaks tests
- Line does not implement equality operator HOT 6
- MiniTest isn't a default install for Ruby 2.2 HOT 1
- Add 'area' methods
- [Bug] Specific Polygons That Won't Union HOT 1
- Add a workflow that publishes to RubyGems
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from geometry.