_TITLE "Inversion point sub" 'b+ 202-04-14 '2020-04-15 Oh man! there is a way way easier way to do this!!! yep!
CONST xmax
= 1200, ymax
= 700
CIRCLE (xmax
/ 2, ymax
/ 2), 200
CIRCLE (mx
, my
), 2, &HFFF0000FF 'test new sub
inversionPoint xmax / 2, ymax / 2, 200, mx, my, Rx, Ry
CIRCLE (Rx
, Ry
), 2, &HFFFFFF00 'yellow circle should be on line perprendicular to 3rd click point
SUB inversionPoint
(CX
, CY
, CR
, X
, Y
, IX
, IY
) 'update the easy way 2020-04-15 aPrime = CR ^ 2 / a
IX
= CX
+ aPrime
* COS(ang
) IY
= CY
+ aPrime
* SIN(ang
) IX = CX: IY = CY 'because a line across diamter is inverted to itself
SUB inversionPointOrig
(CX
, CY
, CR
, X
, Y
, IX
, IY
)
IX = X: IY = Y
ELSEIF dist
> CR
THEN 'find angle of tangent line from x, y to circle radius ELSE 'dist < r x, y is inertenal and need to find external a
= _ATAN2(Y
- CY
, X
- CX
) - _PI / 2 'perpendicular to X, Y and CX, CY 'need another point on the line perpendicular at x, y
x1
= X
+ 100 * COS(a
): y1
= Y
+ 100 * SIN(a
) CIRCLE (x1
, y1
), 2, &HFF0000FF
' two points make a line find where it intersects circle
nPts = lineIntersectCircle%(X, Y, x1, y1, CX, CY, CR, ix1, iy1, ix2, iy2)
IF nPts
= 2 THEN 'now we need 2 points to line perp to radius at ix1, ix2 CIRCLE (ix1
, iy1
), 2, &HFF00AA00:
CIRCLE (ix2
, iy2
), 2, &HFF00AA00 'green circle intersect OK
a
= _ATAN2(CY
- iy1
, CX
- ix1
) - _PI / 2 ' the angle of tangent point to center of circle x2
= ix1
+ 100 * COS(a
): y2
= iy1
+ 100 * SIN(a
) ' extend tangent line in some direction
'check work
LINE (x2
, y2
)-(ix1
, iy1
), &HFF0000FF CIRCLE (ix1
, iy1
), 4, &HFF0000FF ' tangent point from x, y perpendicular
' intersect of 2 lines
nPoints = lineIntersectLine%(CX, CY, X, Y, ix1, iy1, x2, y2, IX, IY)
IF nPoints
<> 1 THEN IX
= 0: IY
= 0:
BEEP '???? no intersect
SUB slopeYintersect
(X1
, Y1
, X2
, Y2
, slope
, Yintercept
) ' fix for when x1 = x2 slope = X1
Yintercept = Y2
slope = (Y2 - Y1) / (X2 - X1)
Yintercept = slope * (0 - X1) + Y1
SUB PointOnLinePerp2Point
(Lx1
, Ly1
, Lx2
, Ly2
, Px
, Py
, Rx
, Ry
) '
'this sub needs SUB slopeYintersect (X1, Y1, X2, Y2, slope, Yintercept) ' fix for when x1 = x2
'
'Lx1, Ly1, Lx2, Ly2 the two points that make a line
'Px, Py is point off the line
'Rx, Ry Return Point is the Point on the line perpendicular to Px, Py
slopeYintersect Lx1, Ly1, Lx2, Ly2, m, Y0
A = m ^ 2 + 1
B = 2 * (m * Y0 - m * Py - Px)
Rx = -B / (2 * A)
Ry = m * Rx + Y0
FUNCTION lineIntersectLine%
(ax1
, ay1
, ax2
, ay2
, bx1
, by1
, bx2
, by2
, ix
, iy
) IF ax1
= ax2
THEN 'line a is vertical IF bx1
= bx2
THEN ' b is vertical IF ax1
= bx1
THEN lineIntersectLine%
= -1 ' if x's are same it is same vertical line ix = ax1
slopeYintersect bx1, by1, bx2, by2, m2, y02
iy = m2 * ix + y02
lineIntersectLine% = 1 'signal a point was found
slopeYintersect ax1, ay1, ax2, ay2, m1, y01 ' -m = a, 1 = b, y0 = c std form
IF bx1
= bx2
THEN 'b is vertical ix = bx1: iy = m1 * ix + y01: lineIntersectLine% = 1 'signal a point was found
slopeYintersect bx1, by1, bx2, by2, m2, y02 ' -m = a, 1 = b, y0 = c std form
d = -m1 - -m2 ' if = 0 then parallel or equal because slopes are same
ix = (y01 - y02) / d: iy = (-m1 * y02 - -m2 * y01) / d
lineIntersectLine% = 1 'signal one intersect point was found
ELSE 'same line or parallel? if y0 are same they are the same IF ABS(y01
- y02
) < 5 THEN lineIntersectLine%
= -1 'signal same line!
' return 0 no Intersect, 1 = tangent 1 point touch, 2 = 2 point intersect
FUNCTION lineIntersectCircle%
(lx1
, ly1
, lx2
, ly2
, cx
, cy
, r
, ix1
, iy1
, ix2
, iy2
)
'needs SUB slopeYintersect (X1, Y1, X2, Y2, slope, Yintercept)
slopeYintersect lx1, ly1, lx2, ly2, m, Y0 ' Y0 otherwise know as y Intersect
' https://math.stackexchange.com/questions/228841/how-do-i-calculate-the-intersections-of-a-straight-line-and-a-circle
A = m ^ 2 + 1
B = 2 * (m * Y0 - m * cy - cx)
C = cy ^ 2 - r ^ 2 + cx ^ 2 - 2 * Y0 * cy + Y0 ^ 2
D = B ^ 2 - 4 * A * C 'telling part of Quadratic formula = 0 then circle is tangent or > 0 then 2 intersect points
IF D
< 0 THEN ' no intersection ix1 = -999: iy1 = -999: ix2 = -999: iy2 = -999: lineIntersectCircle% = 0
x1
= (-B
+ SQR(D
)) / (2 * A
) y1 = m * x1 + Y0
ix1 = x1: iy1 = y1: ix2 = -999: iy2 = -999: lineIntersectCircle% = 1
x1
= (-B
+ SQR(D
)) / (2 * A
): y1
= m
* x1
+ Y0
x2
= (-B
- SQR(D
)) / (2 * A
): y2
= m
* x2
+ Y0
ix1 = x1: iy1 = y1: ix2 = x2: iy2 = y2: lineIntersectCircle% = 2
ix1 = lx1: iy1 = cy: ix2 = -999: iy2 = -999: lineIntersectCircle% = 1
ix1 = -999: iy1 = -999: ix2 = -999: iy2 = -999: lineIntersectCircle% = 0
ydist
= SQR(r
^ 2 - (lx1
- cx
) ^ 2) ix1 = lx1: iy1 = cy + ydist: ix2 = lx1: iy2 = cy - ydist: lineIntersectCircle% = 2