Skip to content

Commit

Permalink
triangle tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cormullion committed Jan 7, 2021
1 parent b585a80 commit 3cfae3e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 74 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Changelog

## [v2.7.0] - January 2021 probably.
## [v2.7.0] - January 7 2021

### Added

- triangle functions
- perpendicular() bisector
- macros allow variables (thanks Mason!)

### Changed

Expand Down
81 changes: 8 additions & 73 deletions src/point.jl
Original file line number Diff line number Diff line change
Expand Up @@ -307,88 +307,23 @@ function ispointonline(pt::Point, pt1::Point, pt2::Point;
end
end

# """
# intersection(p1::Point, p2::Point, p3::Point, p4::Point;
# commonendpoints = false,
# crossingonly = false,
# collinearintersect = false)
#
# Find intersection of two lines `p1`-`p2` and `p3`-`p4`
#
# Deprecated. Use `intersectionlines().`
# """
# function intersection(A::Point, B::Point, C::Point, D::Point;
# commonendpoints = false,
# crossingonly = false,
# collinearintersect = false
# )
# # false if either line is undefined
# if (A == B) || (C == D)
# return (false, Point(0, 0))
# end
#
# # false if the lines share a common end point
# if commonendpoints
# if (A == C) || (B == C) || (A == D) || (B == D)
# return (false, Point(0, 0))
# end
# end
#
# # Cramer's ?
# A1 = (A.y - B.y)
# B1 = (B.x - A.x)
# C1 = (A.x * B.y - B.x * A.y)
#
# L1 = (A1, B1, -C1)
#
# A2 = (C.y - D.y)
# B2 = (D.x - C.x)
# C2 = (C.x * D.y - D.x * C.y)
#
# L2 = (A2, B2, -C2)
#
# d = L1[1] * L2[2] - L1[2] * L2[1]
# dx = L1[3] * L2[2] - L1[2] * L2[3]
# dy = L1[1] * L2[3] - L1[3] * L2[1]
#
# # if you ask me collinear points don't really intersect
# if (C1 == C2) && (collinearintersect == false)
# return (false, Point(0, 0))
# end
#
# if d != 0
# pt = Point(dx/d, dy/d)
# if crossingonly == true
# if ispointonline(pt, A, B) && ispointonline(pt, C, D)
# return (true, pt)
# else
# return (false, pt)
# end
# else
# if ispointonline(pt, A, B, extended=true) && ispointonline(pt, C, D, extended=true)
# return (true, pt)
# else
# return (false, pt)
# end
# end
# else
# return (false, Point(0, 0))
# end
# end

"""
slope(pointA::Point, pointB::Point)
Find angle of a line starting at `pointA` and ending at `pointB`.
Return a value between 0 and 2pi. Value will be relative to the current axes.
slope(O, Point(0, 100)) |> rad2deg # y is positive down the page
90.0
```
slope(O, Point(0, 100)) |> rad2deg # y is positive down the page
90.0
slope(Point(0, 100), O) |> rad2deg
270.0
slope(Point(0, 100), O) |> rad2deg
270.0
```
The slope isn't the same as the gradient. A vertical line going up has a
slope of 3π/2.
"""
function slope(pointA, pointB)
return mod2pi(atan(pointB.y - pointA.y, pointB.x - pointA.x))
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ function run_all_tests()
include("boxmaptest.jl")
include("noise-test.jl")
include("dashtests.jl")
include("triangles.jl")
end
end

Expand Down
58 changes: 58 additions & 0 deletions test/triangles.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env julia

using Luxor

using Test

using Random
Random.seed!(42)

function triangle_tests(fname)
Drawing(800, 800, fname)
origin()
background("white")
fontsize(10)

panes = Tiler(500, 500, 1, 2)

@layer begin
translate(first(panes[1]))
# equilateral
tri1 = [polar(100, θ) for θ in (0, 2π/3, 4π/3)]
prettypoly(tri1, :stroke, close=true)

orthocenter = triangleorthocenter(tri1...)
incenter = triangleincenter(tri1...)
center = trianglecenter(tri1...)
circumcenter = trianglecircumcenter(tri1...)
circle.((orthocenter, incenter, center, circumcenter), 2, :fill)

# all centers are the same
@test isapprox(orthocenter, incenter)
@test isapprox(orthocenter, center)
@test isapprox(orthocenter, circumcenter)
end

@layer begin
translate(first(panes[2]))
# isosceles
tri2 = [Point(0, 0), Point(80, -80), Point(160, 0)]
prettypoly(tri2, :stroke, close=true)

orthocenter = triangleorthocenter(tri2...)
incenter = triangleincenter(tri2...)
center = trianglecenter(tri2...)
circumcenter = trianglecircumcenter(tri2...)
circle.((orthocenter, incenter, center, circumcenter), 2, :fill)

# all centers lie on the same vetical line
@test isapprox(orthocenter.x, incenter.x)
@test isapprox(orthocenter.x, center.x)
@test isapprox(orthocenter.x, circumcenter.x)
end

@test finish() == true
println("...finished test: output in $(fname)")
end

triangle_tests("triangle-tests.svg")

0 comments on commit 3cfae3e

Please sign in to comment.