Distances
Euclidean distance
PlaneGeometry.distance
— Functiondistance(A, B)
Compute the euclidean distance between A
and B
, which is the "ordinary" straight-line distance between two points.
Examples
julia> distance(Point(0, 0), Point(3,4))
5
Source Code
function distance(A, B)
SymPy.sqrt(squaredist(A, B))
end
Squared Euclidean distance
PlaneGeometry.squaredist
— Functionsquaredist(A, B)
Compute the square of the euclidean distance between A
and B
.
Examples
julia> squaredist(Point(0, 0), Point(2,2))
8
Source Code
function squaredist(A, B)
d = (A.x-B.x)^2+(A.y-B.y)^2
simplify(Sym(d))
end
Midpoint of an edge
PlaneGeometry.midpoint
— Functionmidpoint(A, B)
Find the midpoint between A
and B
.
Examples
julia> midpoint(Point(0, 0), Point(2,4))
Point(1, 2)
Source Code
function midpoint(A, B)
x1 = (A.x + B.x)/2
y1 = (A.y + B.y)/2
Point(x1, y1)
end
Picture
plot(Edge(A, B), leg=false, color=:orange)
mid = midpoint(A, B)
scatter!(shape([A, B, mid]), leg=false, aspect_ratio=:equal,
color=[:red, :red, :green], series_annotations = text.(["A", "B", "mid"], :bottom))
Concurrent Point
PlaneGeometry.concurrent
— Methodconcurrent(edgelist::Vector{Edge})
Find the point where all edges (lines) in edgelist
intersect if such point exists. Return nothing otherwise.
Examples
julia> concurrent([Edge(Point(0,1), Point(1,1)), Edge(Point(0,1), Point(1,0))])
Point(0, 1)
PlaneGeometry.concurrent
— Methodconcurrent(e1::Edge, e2::Edge)
Find the point the edges e1
and e2
intersect if such point exists. Return nothing otherwise.
Examples
julia> concurrent(Edge(Point(0,1), Point(1,1)), Edge(Point(0,1), Point(1,0)))
Point(0, 1)
Source Code
function concurrent(edgelist::Vector{Edge})
enum = length(edgelist)
if enum == 1
throw(ArgumentError("At least two edges are needed. $enum is given."))
end
@vars x y
eqs = Sym[]
for e in edgelist
# This should be 0
eq = (x-e.src.x)*(y-e.dst.y) - (x-e.dst.x)*(y-e.src.y)
push!(eqs, eq)
end
sol = solve(eqs, [x,y])
if length(sol) == 0
return nothing
else
return Point(simplify(sol[x]), simplify(sol[y]))
end
end
Picture
elist = map(i->median(circshift([A, B, C], i)...), 0:2)
cpt = concurrent(elist)
plot(Triangle(A, B, C), fill=(0, :green), aspect_ratio=:equal, fillalpha= 0.2)
for e in elist
plot!(e, leg=false, color=:orange)
end
scatter!(shape([A, B, C, cpt]),
leg=false,
aspect_ratio=:equal,
color=[:red, :red, :red, :green],
series_annotations = text.(["A", "B", "C", "cpt"], :bottom))