_TITLE "Triangle Dissection 2 user click" 'B+ 2020-01-29 ' Turn a triangle into a square (and back)
' 2020-01-30 now for any triangle, oh and swap points around until back to original dissection! nice :)
' 2020-01-30 Oh now let user click his own triangle for dissection
CONST xmax
= 800, ymax
= 740, blu
= &H880000FF, red
= &H88FF0000
DIM Ax
, Ay
, Fx
, Fy
, Jx
, Jy
'3 corners A is apex, F and J form iso triangle DIM Bx
, By
, Cx
, Cy
'midpoint AF and AJ DIM Gx
, Gy
, Hx
, Hy
'1/4 lengths of base DIM distFJ
, aJ
' to calc points G and H DIM Dx
, Dy
, Ex
, Ey
'two crital points for forming 90 degree angles DIM D2x
, D2y
, E2x
, E2y
, G2x
, G2y
'copy points to move as independent blocks DIM a
, cnt
, cc
'a = angle in degrees loop counter, cycle counter DIM tx
, ty
' for temp holders to swap points 3 way swap not 2 way DIM mx
(3), my
(3), pi
, oldMouse
'for mouse user input
getUserTri:
cc = 0
WHILE pi
< 3 'get 3 mouse clicks IF SQR((mx
(0) - 400) ^ 2 + (my
(0) - 370) ^ 2) < 200 THEN pi = pi + 1
mx(pi) = mx(0): my(pi) = my(0)
Ax = mx(1): Ay = my(1)
Jx = mx(2): Jy = my(2)
Fx = mx(3): Fy = my(3)
'initial triangle
'Ax = 400: Ay = 200: Fx = 200: Fy = 500: Jx = 600: Jy = 500 'jx = 600, jy = 500
restart:
cc = cc + 1
Bx = (Ax + Fx) / 2: By = (Ay + Fy) / 2: Cx = (Ax + Jx) / 2: Cy = (Ay + Jy) / 2
distFJ
= _HYPOT(Fx
- Jx
, Fy
- Jy
)Gx
= Fx
+ .25 * distFJ
* COS(aJ
)Gy
= Fy
+ .25 * distFJ
* SIN(aJ
)Hx
= Fx
+ .75 * distFJ
* COS(aJ
)Hy
= Fy
+ .75 * distFJ
* SIN(aJ
)circleTangentXY Gx, Gy, Cx, Cy, Bx, By, Dx, Dy
circleTangentXY Gx, Gy, Cx, Cy, Hx, Hy, Ex, Ey
D2x = Dx: D2y = Dy
E2x = Ex: E2y = Ey
G2x = Gx: G2y = Gy
'draw traingle for check
'ln Ax, Ay, Fx, Fy
'ln Ax, Ay, Jx, Jy
'ln Fx, Fy, Jx, Jy
'ln Gx, Gy, Cx, Cy
'ln Dx, Dy, Bx, By
'ln Ex, Ey, Hx, Hy
'_DISPLAY
'_DELAY 1
'draw our starter triangle
fquad Ax, Ay, Cx, Cy, Dx, Dy, Bx, By, blu
fquad Bx, By, D2x, D2y, Gx, Gy, Fx, Fy, blu
fquad Cx, Cy, Jx, Jy, Hx, Hy, Ex, Ey, blu
ftri Hx, Hy, G2x, G2y, E2x, E2y, blu
'start dissection with all points needed
a = 1: cnt = 0
fquad Ax, Ay, Cx, Cy, Dx, Dy, Bx, By, blu
rotate D2x, D2y, Bx, By, a
rotate Gx, Gy, Bx, By, a
rotate Fx, Fy, Bx, By, a
fquad Bx, By, D2x, D2y, Gx, Gy, Fx, Fy, blu
rotate Jx, Jy, Cx, Cy, -a
rotate Hx, Hy, Cx, Cy, -a
rotate Ex, Ey, Cx, Cy, -a
fquad Cx, Cy, Jx, Jy, Hx, Hy, Ex, Ey, blu
rotate G2x, G2y, Cx, Cy, -a
rotate E2x, E2y, Cx, Cy, -a
ftri Hx, Hy, G2x, G2y, E2x, E2y, blu
cnt = cnt + 1
cnt = 0
fquad Ax, Ay, Cx, Cy, Dx, Dy, Bx, By, blu
fquad Bx, By, D2x, D2y, Gx, Gy, Fx, Fy, blu
fquad Cx, Cy, Jx, Jy, Hx, Hy, Ex, Ey, blu
rotate G2x, G2y, Hx, Hy, -a
rotate E2x, E2y, Hx, Hy, -a
ftri Hx, Hy, G2x, G2y, E2x, E2y, blu
cnt = cnt + 1
cnt = 0
fquad Ax, Ay, Cx, Cy, Dx, Dy, Bx, By, blu
fquad Bx, By, D2x, D2y, Gx, Gy, Fx, Fy, blu
fquad Cx, Cy, Jx, Jy, Hx, Hy, Ex, Ey, blu
rotate G2x, G2y, Hx, Hy, a
rotate E2x, E2y, Hx, Hy, a
ftri Hx, Hy, G2x, G2y, E2x, E2y, blu
cnt = cnt + 1
cnt = 0
fquad Ax, Ay, Cx, Cy, Dx, Dy, Bx, By, blu
rotate D2x, D2y, Bx, By, -a
rotate Gx, Gy, Bx, By, -a
rotate Fx, Fy, Bx, By, -a
fquad Bx, By, D2x, D2y, Gx, Gy, Fx, Fy, blu
rotate Jx, Jy, Cx, Cy, a
rotate Hx, Hy, Cx, Cy, a
rotate Ex, Ey, Cx, Cy, a
fquad Cx, Cy, Jx, Jy, Hx, Hy, Ex, Ey, blu
rotate G2x, G2y, Cx, Cy, a
rotate E2x, E2y, Cx, Cy, a
ftri Hx, Hy, G2x, G2y, E2x, E2y, blu
cnt = cnt + 1
'swap points for different dissection
tx = Ax: ty = Ay
Ax = Jx: Ay = Jy
Jx = Fx: Jy = Fy
Fx = tx: Fy = ty
SUB rotate
(x
, y
, cx
, cy
, rAngle
) 'replace x, y with new position angle
= _ATAN2(y
- cy
, x
- cx
) distance = ((x - cx) ^ 2 + (y - cy) ^ 2) ^ .5
x
= cx
+ distance
* COS(angle
+ _D2R(rAngle
)) y
= cy
+ distance
* SIN(angle
+ _D2R(rAngle
))
SUB circleTangentXY
(X1
, Y1
, X2
, Y2
, xC
, yC
, findXperp
, findYperp
) 'p1 and p2 form a line, with slop and y intersect y0
'xC, yC is a circle origin
'we find X, Y such that line x, y to xC, yC is perpendicular to p1, p2 line that is radius of tangent circle
slope = (Y2 - Y1) / (X2 - X1)
y0 = slope * (0 - X1) + Y1
A = slope ^ 2 + 1
B = 2 * (slope * y0 - slope * yC - xC)
findXperp = -B / (2 * A)
findYperp = slope * findXperp + y0
findXperp = X1
findYperp = yC
'SUB drawLine (x1, y1, x2, y2, K AS _UNSIGNED LONG)
' slope = (y2 - y1) / (x2 - x1)
' y0 = slope * (0 - x1) + y1
' LINE (0, y0)-(_WIDTH, slope * _WIDTH + y0), &HFF0000FF
'END SUB
'2019-12-16 fix by Steve saves some time with STATIC and saves and restores last dest
_BLEND a&
'<<<< new 2019-12-16 fix
'update 2019-12-16 needs updated fTri 2019-12-16
'need 4 non linear points (not all on 1 line) list them clockwise so x2, y2 is opposite of x4, y4
ftri x1, y1, x2, y2, x3, y3, K
ftri x3, y3, x4, y4, x1, y1, K