问题
Sorry for my language since English is my second language.
I am trying to convert a straight line into a fractal known as Koch curve. The 2 points of the straight line are given and then I need to create the Koch curve where I divide the line to 3 segments and then make the second segment an equilateral triangle. See http://www.tgmdev.be/curvevonkoch.php.
So far we convert the straight line to 4 equally segments, and I need to figure out all the coordinates of the Koch curve.
I have thought of a straight line when the y coordinates of the 2 point are the same which give me horizontal line. if so, I can figure out the 3 points of equilateral triangle by dividing the second segment half and taking the cos(60) of the right triangle. as here: http://www.themathpage.com/atrig/30-60-90-triangle.htm
My problem is how to find all coordinates when the stright line is diagonal, for example a(200,100), b(400,600) or a(400,500), b(100,500).
回答1:
If your base segment is AB, with A(Ax,Ay) and B(Bx,By), then the 4 sub-segments will be AP, PQ, QR, RB as defined below.
First define two orthogonal vectors of same length:
U(Bx-Ax,By-Ay) and
V(Ay-By,Bx-Ax)
Then the points:
P=A+(1/3)*U
Q=A+(1/2)*U+(sqrt(3)/6)*V
R=A+(2/3)*U
The point+vector=point notation is similar to a translation.
Example with A(100,100) and B(400,100):
U(300,0)
V(0,300)
P = (100,100) + (1/3)*(300,0) = (200,100)
Q = (100,100) + (1/2)*(300,0) + (sqrt(3)/6)*(0,300) = (250,186)
R = (100,100) + (2/3)*(300,0) = (300,100)
回答2:
Here is a javascript function based on Eric's algorithm.
export function getChildLinePoints (points, depth = 0) {
if (depth === 0) {
const Ax = points[0]
const Ay = points[1]
const Bx = points[2]
const By = points[3]
const Ux = Bx - Ax
const Uy = By - Ay
const Vx = Ay - By
const Vy = Bx - Ax
const Px = Ax + ((1 / 3) * Ux)
const Py = Ay + ((1 / 3) * Uy)
const Qx = Ax + ((1 / 2) * Ux) + ((Math.sqrt(3) / 6) * Vx)
const Qy = Ay + ((1 / 2) * Uy) + ((Math.sqrt(3) / 6) * Vy)
const Rx = Ax + ((2 / 3) * Ux)
const Ry = Ay + ((2 / 3) * Uy)
return [[
Ax, Ay,
Px, Py
], [
Px, Py,
Qx, Qy
], [
Qx, Qy,
Rx, Ry
], [
Rx, Ry,
Bx, By
]]
} else {
const xpoints = [...getChildLinePoints(points, depth - 1)]
return xpoints.reduce((acc, point) => [...acc, ...getChildLinePoints(point)], [])
}
}
来源:https://stackoverflow.com/questions/15367165/finding-coordinates-of-koch-curve