Divide a rectangle into a number of unequal triangles: Difference between revisions

Line 11:
* Explain, or refer to the algorithm employed.
* Give a sample of sets of triangles produced from running the algorithm, on this page.
 
 
=={{header|Julia}}==
The `cutrectangle` method creates a new triangle by consuming a previously created triangle by cutting it at a location determined by the ratio of two sequential primes, making for guaranteed noncongruence of all triangles thus made. The Luxor code is for the demo.
<lang julia>using Colors
using Luxor
using Primes
using Random
 
mutable struct Triangle
v1::Point
v2::Point
v3::Point
end
 
function cutrectangle(A, B, C, D, n)
n < 2 && error("Cannot cut rectangle into < 2 triangles")
triangles, i, j = [Triangle(A, B, C), Triangle(C, D, A)], 1, 2
for _ in 1:n
t = popfirst!(triangles)
p = Point(t.v3.x + 1/i * (t.v1.x - t.v3.x), t.v3.y + 1/i * (t.v1.y - t.v3.y))
push!(triangles, Triangle(t.v1, t.v2, p), Triangle(t.v2, t.v3, p))
i, j = j, nextprime(j)
end
return triangles
end
 
colors = shuffle(collect(Colors.color_names))
idx = 1
Drawing(500, 500)
origin()
 
triangles = cutrectangle(Point(0.0, 0.0), Point(200.0, 0.0), Point(200.0, 200.0), Point(0.0, 200.0), 9)
 
for t in triangles
sethue(colors[mod1(idx, length(colors))][1])
poly([t.v1, t.v2, t.v3], :fill)
global idx += 1
end
 
finish()
preview()
</lang>{{out}}
<pre>
t = Triangle(Point(200.0, 200.0), Point(0.0, 0.0), Point(100.0, 0.0))
t = Triangle(Point(200.0, 200.0), Point(0.0, 200.0), Point(150.0, 150.0))
t = Triangle(Point(0.0, 200.0), Point(100.0, 100.0), Point(150.0, 150.0))
t = Triangle(Point(0.0, 200.0), Point(0.0, 0.0), Point(50.0, 150.0))
t = Triangle(Point(0.0, 0.0), Point(100.0, 100.0), Point(50.0, 150.0))
t = Triangle(Point(0.0, 0.0), Point(200.0, 0.0), Point(0.0, 0.0))
t = Triangle(Point(200.0, 0.0), Point(0.0, 0.0), Point(0.0, 0.0))
t = Triangle(Point(200.0, 0.0), Point(0.0, 0.0), Point(100.0, 0.0))
t = Triangle(Point(0.0, 0.0), Point(0.0, 0.0), Point(100.0, 0.0))
t = Triangle(Point(200.0, 0.0), Point(200.0, 200.0), Point(150.0, 0.0))
t = Triangle(Point(200.0, 200.0), Point(100.0, 0.0), Point(150.0, 0.0))
</pre>
 
=={{header|Phix}}==
4,103

edits