_TITLE "Interior of an Angle" ' B+ 2020-06-09 Math Geometry ' Interesting problem posed by STx 2020-06-17
' https://www.qb64.org/forum/index.php?topic=2714.0
'got me thinking how I might solve it here is what I came uo with
' to save time using Steve's code to problem setup, so I check same as his for starters
px = 160: py = 150
tf = PtInteriorOfTriangleTF(100, 100, 200, 100, 300, 300, px, py)
px = 150: py = 50
tf = PtInteriorOfTriangleTF(100, 100, 200, 100, 300, 300, px, py)
'try some more borderline cases
' pt is corner of tringle
PRINT "Test triangle point" px = 100: py = 100
tf = PtInteriorOfTriangleTF(100, 100, 200, 100, 300, 300, px, py)
'point is on line between 2 points
px = 150: py = 100
tf = PtInteriorOfTriangleTF(100, 100, 200, 100, 300, 300, px, py)
'oh let's just test everywhere!
_TITLE "Testing random points on screen, press escape to quit any other for another test..." px
= RND * 640: py
= RND * 480 tf = PtInteriorOfTriangleTF(100, 100, 200, 100, 300, 300, px, py)
SUB drawSituation
(tx1
, ty1
, tx2
, ty2
, tx3
, ty3
, px
, py
) '3 points of triangle and test pt yellow LINE (tx1
, ty1
)-(tx2
, ty2
) LINE (tx2
, ty2
)-(tx3
, ty3
) LINE (tx3
, ty3
)-(tx1
, ty1
) CIRCLE (px
, py
), 3, &HFFFFFF00
FUNCTION PtInteriorOfTriangleTF
(tx1
, ty1
, tx2
, ty2
, tx3
, ty3
, px
, py
) drawSituation tx1, ty1, tx2, ty2, tx3, ty3, px, py
IF PtInteriorOfAngleTF
(tx1
, ty1
, tx2
, ty2
, tx3
, ty3
, px
, py
) THEN IF PtInteriorOfAngleTF
(tx2
, ty2
, tx1
, ty1
, tx3
, ty3
, px
, py
) THEN IF PtInteriorOfAngleTF
(tx3
, ty3
, tx1
, ty1
, tx2
, ty2
, px
, py
) THEN PtInteriorOfTriangleTF = -1
FUNCTION PtInteriorOfAngleTF
(ox1
, oy1
, ax1
, ay1
, ax2
, ay2
, ptx
, pty
) a1
= atan360
(_ATAN2(ay1
- oy1
, ax1
- ox1
)) a2
= atan360
(_ATAN2(ay2
- oy1
, ax2
- ox1
)) ap
= atan360
(_ATAN2(pty
- oy1
, ptx
- ox1
)) IF ap
>= a1
AND ap
<= a2
THEN PtInteriorOfAngleTF
= -1
'PRINT a1, a2, ap, PtInteriorOfAngleTF 'debug
'I can't believe all the blunders I've made putting this so simple an idea together!!!
FUNCTION atan360
(ra
) 'convert radian angle from _ATAN2() to guaranteed positive angle
'FUNCTION CheckTriangle (tx1, ty1, tx2, ty2, tx3, ty3, px, py)
'thanks SMcNeill for this simple to understand method
' BUT I think I have simpler! ;-))
' DIM p AS _UNSIGNED LONG, r AS _UNSIGNED LONG
' d = _DEST: s = _SOURCE
' temp = _NEWIMAGE(_WIDTH, _HEIGHT, 32)
' _DEST temp: _SOURCE temp
' COLOR _RGBA32(255, 255, 255, 100)
' _DONTBLEND
' LINE (tx1, ty1)-(tx2, ty2)
' LINE (tx2, ty2)-(tx3, ty3)
' LINE (tx3, ty3)-(tx1, ty1)
' txc = (tx1 + tx2 + tx3) / 3 'triangle x center
' tyc = (ty1 + ty2 + ty3) / 3 'triangle y center
' PAINT (txc, tyc)
' _BLEND
' PSET (px, py), _RGBA(255, 0, 0, 200)
' p = POINT(px, py)
' IF p = _RGBA32(255, 55, 55, 222) THEN CheckTriangle = -1
' _DEST d: _SOURCE s
' IF DRAWIT THEN
' CLS
' CIRCLE (px, py), 5, _RGBA(255, 0, 0, 200)
' _PUTIMAGE (0, 0), temp, d
' END IF
' _FREEIMAGE temp
'END FUNCTION