Find the intersection of two lines: Difference between revisions

Content added Content deleted
Line 1,149: Line 1,149:
class line {
class line {
private:
private:
slop, Xz, Yz
slop, k
public:
public:
function f(x) {
function f(x) {
=.slop*(x-.xz)+.yz
=x*.slop-.k
}
}
function intersection(b as line) {
function intersection(b as line) {
Line 1,158: Line 1,158:
=(,)
=(,)
else
else
x1=(-b.xz*b.slop+b.yz+.xz*.slop-.yz)/(.slop-b.slop)
x1=(.k-b.k)/(.slop-b.slop)
=(x1, .f(x1))
=(x1, .f(x1))
end if
end if
Line 1,167: Line 1,167:
if x1==x2 then error "wrong input"
if x1==x2 then error "wrong input"
if x1>x2 then swap x1,x2 : swap y1, y2
if x1>x2 then swap x1,x2 : swap y1, y2
(.slop, .xz, .yz)=((y1-y2)/(x1-x2), x1, y1)
.slop<=(y1-y2)/(x1-x2)
.k<=x1*.slop-y1
}
}
}
}