Author Topic: Harder-than-it-looks math problem  (Read 9696 times)

0 Members and 1 Guest are viewing this topic.

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #15 on: June 19, 2020, 07:03:43 am »
@SMcNeill why do you the PSET at all? Wouldn't it just be easier to check the POINT directly:
Code: [Select]
FUNCTION CheckTriangle (tx1, ty1, tx2, ty2, tx3, ty3, px, py)
    DIM p AS _UNSIGNED LONG, r AS _UNSIGNED LONG
    d = _DEST: s = _SOURCE
    temp = _NEWIMAGE(_WIDTH, _HEIGHT, 32)
    _DEST temp: _SOURCE temp
    COLOR _RGB(255, 255, 255)
    _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)
    CheckTriangle = POINT(px, py) > 0

    _DEST d: _SOURCE s
    IF DRAWIT THEN
        CLS
        CIRCLE (px, py), 5, _RGB(255, 0, 0)
        _PUTIMAGE (0, 0), temp, d
    END IF
    _FREEIMAGE temp
END FUNCTION
Or am I missing something?

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #16 on: June 19, 2020, 07:16:22 am »
Thanks, QB64, for _ACOS() additional to the restrictive ATN() of QuickBasic.  My method uses The Law of Cosines to find angles from triangle side lengths.

To keep the primed variables from measuring the wrong angle you can force them to be less than pi.

The beauty of _ACOS() is that it always returns the angle in the range 0 - pi, and so needs no code adjusters.
Next time, I'll have a slightly harder trig puzzle!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #17 on: June 19, 2020, 01:42:35 pm »
Nice one @luke  I suspected there was easier way to Steve's method but blundered, my mind probably too cluttered with worries about my gambling problem with Blackjack ;-))

Here is key to the simplest solution (so far), well @MasterGy has fewest Lines of Code (I think, don't see working demo) but why does that work? assuming it does, knowing MasterGy I think I am safe ;-))
I am also assuming Luke's also works because..., well you know!
BTW I am also assuming STx's works for same reason. :)
I only tried @SMcNeill 's and know it works as far as it went, but why I ask the PAINT question at the end of this post.

Key to simple math solution:
Code: QB64: [Select]
  1. FUNCTION PtInteriorOfAngleTF (ox1, oy1, ax1, ay1, ax2, ay2, ptx, pty)
  2.     a1 = atan360(_ATAN2(ay1 - oy1, ax1 - ox1)) ' angle of one arm from origin of angle
  3.     a2 = atan360(_ATAN2(ay2 - oy1, ax2 - ox1)) ' angle of the other arm from origin of angle
  4.     ap = atan360(_ATAN2(pty - oy1, ptx - ox1)) ' angle of point from same origin
  5.     IF a2 < a1 THEN SWAP a2, a1 'make a1 the samller of the two angles for test fit of point
  6.     IF ap >= a1 AND ap <= a2 THEN PtInteriorOfAngleTF = -1 ' if point angle is inside or on the 2 arm angles then point is in or on.
  7.  
  8.     'PRINT a1, a2, ap, PtInteriorOfAngleTF 'debug
  9.     'I can't believe all the blunders I've made putting this so simple an idea together!!!
  10.  
  11. FUNCTION atan360 (ra) 'convert radian angle from _ATAN2()  to guaranteed positive angle
  12.     atan360 = _R2D(ra + _PI(2))
  13.  

Here it is in action to the triangle problem:
Code: QB64: [Select]
  1. _TITLE "Interior of an Angle" ' B+ 2020-06-09  Math Geometry
  2. ' Interesting problem posed by STx 2020-06-17
  3. ' https://www.qb64.org/forum/index.php?topic=2714.0
  4.  
  5. 'got me thinking how I might solve it here is what I came uo with
  6. ' to save time using Steve's code to problem setup, so I check same as his for starters
  7.  
  8. SCREEN _NEWIMAGE(640, 480, 32)
  9. px = 160: py = 150
  10. PRINT "Test Interior"
  11. tf = PtInteriorOfTriangleTF(100, 100, 200, 100, 300, 300, px, py)
  12. IF tf THEN PRINT "Pt ("; px; ","; py; ") is in or on." ELSE PRINT "Pt ("; px; ","; py; ") is Out."
  13. px = 150: py = 50
  14. PRINT "Test Exterior"
  15. tf = PtInteriorOfTriangleTF(100, 100, 200, 100, 300, 300, px, py)
  16. IF tf THEN PRINT "Pt ("; px; ","; py; ") is in or on." ELSE PRINT "Pt ("; px; ","; py; ") is Out."
  17.  
  18. 'try some more borderline cases
  19. ' pt is corner of tringle
  20. PRINT "Test triangle point"
  21. px = 100: py = 100
  22. tf = PtInteriorOfTriangleTF(100, 100, 200, 100, 300, 300, px, py)
  23. IF tf THEN PRINT "Pt ("; px; ","; py; ") is in or on." ELSE PRINT "Pt ("; px; ","; py; ") is Out."
  24. 'point is on line between 2 points
  25. px = 150: py = 100
  26. PRINT "Test Border line"
  27. tf = PtInteriorOfTriangleTF(100, 100, 200, 100, 300, 300, px, py)
  28. IF tf THEN PRINT "Pt ("; px; ","; py; ") is in or on." ELSE PRINT "Pt ("; px; ","; py; ") is Out."
  29.  
  30. 'oh let's just test everywhere!
  31. _TITLE "Testing random points on screen, press escape to quit any other for another test..."
  32. WHILE _KEYDOWN(27) = 0
  33.     CLS
  34.     px = RND * 640: py = RND * 480
  35.     tf = PtInteriorOfTriangleTF(100, 100, 200, 100, 300, 300, px, py)
  36.     IF tf THEN PRINT "Pt ("; px; ","; py; ") is in or on." ELSE PRINT "Pt ("; px; ","; py; ") is Out."
  37.     SLEEP
  38.  
  39.  
  40. SUB drawSituation (tx1, ty1, tx2, ty2, tx3, ty3, px, py) '3 points of triangle and test pt yellow
  41.     LINE (tx1, ty1)-(tx2, ty2)
  42.     LINE (tx2, ty2)-(tx3, ty3)
  43.     LINE (tx3, ty3)-(tx1, ty1)
  44.     CIRCLE (px, py), 3, &HFFFFFF00
  45.  
  46. FUNCTION PtInteriorOfTriangleTF (tx1, ty1, tx2, ty2, tx3, ty3, px, py)
  47.     drawSituation tx1, ty1, tx2, ty2, tx3, ty3, px, py
  48.     IF PtInteriorOfAngleTF(tx1, ty1, tx2, ty2, tx3, ty3, px, py) THEN
  49.         IF PtInteriorOfAngleTF(tx2, ty2, tx1, ty1, tx3, ty3, px, py) THEN
  50.             IF PtInteriorOfAngleTF(tx3, ty3, tx1, ty1, tx2, ty2, px, py) THEN
  51.                 PtInteriorOfTriangleTF = -1
  52.             END IF
  53.         END IF
  54.     END IF
  55.  
  56. FUNCTION PtInteriorOfAngleTF (ox1, oy1, ax1, ay1, ax2, ay2, ptx, pty)
  57.     a1 = atan360(_ATAN2(ay1 - oy1, ax1 - ox1))
  58.     a2 = atan360(_ATAN2(ay2 - oy1, ax2 - ox1))
  59.     ap = atan360(_ATAN2(pty - oy1, ptx - ox1))
  60.     IF a2 < a1 THEN SWAP a2, a1
  61.     IF ap >= a1 AND ap <= a2 THEN PtInteriorOfAngleTF = -1
  62.  
  63.     'PRINT a1, a2, ap, PtInteriorOfAngleTF 'debug
  64.     'I can't believe all the blunders I've made putting this so simple an idea together!!!
  65.  
  66. FUNCTION atan360 (ra) 'convert radian angle from _ATAN2()  to guaranteed positive angle
  67.     atan360 = _R2D(ra + _PI(2))
  68.  
  69. 'FUNCTION CheckTriangle (tx1, ty1, tx2, ty2, tx3, ty3, px, py)
  70.  
  71. 'thanks SMcNeill for this simple to understand method
  72. ' BUT I think I have simpler! ;-))
  73.  
  74.  
  75. '    DIM p AS _UNSIGNED LONG, r AS _UNSIGNED LONG
  76. '    d = _DEST: s = _SOURCE
  77. '    temp = _NEWIMAGE(_WIDTH, _HEIGHT, 32)
  78. '    _DEST temp: _SOURCE temp
  79. '    COLOR _RGBA32(255, 255, 255, 100)
  80. '    _DONTBLEND
  81. '    LINE (tx1, ty1)-(tx2, ty2)
  82. '    LINE (tx2, ty2)-(tx3, ty3)
  83. '    LINE (tx3, ty3)-(tx1, ty1)
  84. '    txc = (tx1 + tx2 + tx3) / 3 'triangle x center
  85. '    tyc = (ty1 + ty2 + ty3) / 3 'triangle y center
  86. '    PAINT (txc, tyc)
  87. '    _BLEND
  88. '    PSET (px, py), _RGBA(255, 0, 0, 200)
  89. '    p = POINT(px, py)
  90. '    IF p = _RGBA32(255, 55, 55, 222) THEN CheckTriangle = -1
  91. '    _DEST d: _SOURCE s
  92. '    IF DRAWIT THEN
  93. '        CLS
  94. '        CIRCLE (px, py), 5, _RGBA(255, 0, 0, 200)
  95. '        _PUTIMAGE (0, 0), temp, d
  96. '    END IF
  97. '    _FREEIMAGE temp
  98. 'END FUNCTION
  99.  

It is both simple to understand AND mathematical the Intersect of 2 Heavenly Realms.

In English, it just says that a Point Interior to a Triangle has to be Interior to each of it's 3 Interior Angles.

Does PAINT consider a Point On the Triangle in it? Anyway this code considers On or In as opposed to Exterior only.

EDIT: add more comments for key function
« Last Edit: June 19, 2020, 02:25:45 pm by bplus »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #18 on: June 19, 2020, 03:18:54 pm »
Problem with non vector methods is you're stuck on screen. Let's tilt the x and y axes both by say, whatever 31 degrees. How does your solution change?

All my solution does is require that the triangle be drawn with with the right hand rule, and the criteria for being inside is whether or not the stray point occurs to the left of the vector, that's all it does. No explicit coordinate system means this works for any orientation of both the coordinates and the triangle.
You're not done when it works, you're done when it's right.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #19 on: June 19, 2020, 03:59:20 pm »
Wow sorry if I marked that as best answer for a while, don't forum and drive, kids.
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #20 on: June 19, 2020, 04:02:37 pm »
Problem with non vector methods is you're stuck on screen. Let's tilt the x and y axes both by say, whatever 31 degrees. How does your solution change?

All my solution does is require that the triangle be drawn with with the right hand rule, and the criteria for being inside is whether or not the stray point occurs to the left of the vector, that's all it does. No explicit coordinate system means this works for any orientation of both the coordinates and the triangle.

Here we go again with the stupid right hand rule! a 3D property when discussing 2D triangles or shall we talk about triangles on spheres?

A point interior of an angle is not dependent on axis orientation you know that, all the way down to Topology, so I can talk surface of sphere.

Quote
In English, it just says that a Point Interior to a Triangle has to be Interior to each of it's 3 Interior Angles.

My solution depends solely on angles between points.
« Last Edit: June 19, 2020, 04:30:31 pm by bplus »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #21 on: June 19, 2020, 04:58:12 pm »
There are few instances here when someone so blatantly can't see the point. Don't be at the center of it man, it's not usually you.
You're not done when it works, you're done when it's right.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #22 on: June 19, 2020, 05:51:00 pm »
Of course he can't "see" the point. A point is zero-dimensional space. You really need to take my Physic 101 Sems, Bill. :D

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Harder-than-it-looks math problem
« Reply #23 on: June 19, 2020, 08:00:41 pm »
@SMcNeill why do you the PSET at all? Wouldn't it just be easier to check the POINT directly

It would be.  I just added the PSET as a physical representation of the problem.  The code to draw the visible circle to highlight the point came later, as my poor eyes just couldn't pick up on that single pixel very easily.


Quote
Does PAINT consider a Point On the Triangle in it? Anyway this code considers On or In as opposed to Exterior only.

My example counts a point ON the triangle as being IN it.  If that's not desirable for your needs, just redraw the triangles edge a separate color after the fill has did its job.

Quote
Problem with non vector methods is you're stuck on screen. Let's tilt the x and y axes both by say, whatever 31 degrees. How does your solution change?

If you're dealing with 3 dimensional triangles and points, use the GL commands to draw it and check the color value.  The solution really doesn't need to change at all.  People have displayed 3d graphics on 2d screens for ages now.  Didn't you know that??
« Last Edit: June 19, 2020, 08:06:13 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #24 on: June 19, 2020, 08:50:18 pm »
Nope, I have a lot to learn from you about math apparently. Can I start pitching my questions right to you?
« Last Edit: June 19, 2020, 08:51:23 pm by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #25 on: June 19, 2020, 08:56:56 pm »
Actually don't answer, I will pose the same question in 3d next chance I get, that will at least give a chance for bplus to become aminus. I don't know what yer gonna do Steve...
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #26 on: June 19, 2020, 11:43:57 pm »
Dang this is harder than I thought, didn't test enough cases (only one, blah).

Made one fix that repaired test reports for most triangles but still am getting false negatives. I know the theory is right just not executed well enough yet.

Marked as best answer by STxAxTIC on June 19, 2020, 08:09:39 pm

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #27 on: June 20, 2020, 12:04:25 am »
Ok.. Here's my approach. It is based on areas.

EDIT : Code Updated. Now press SpaceBar to generate new triangle
Code: QB64: [Select]
  1. TYPE vec2
  2.     x AS INTEGER
  3.     y AS INTEGER
  4. _TITLE "Move mouse into triangle, it will become red"
  5. SCREEN _NEWIMAGE(600, 600, 32)
  6.  
  7. DIM p(2) AS vec2, m AS vec2
  8. p(0).x = INT(RND * 600): p(0).y = INT(RND * 600)
  9. p(1).x = INT(RND * 600): p(1).y = INT(RND * 600)
  10. p(2).x = INT(RND * 600): p(2).y = INT(RND * 600)
  11. red~& = _RGB(255, 0, 0)
  12. white~& = _RGB32(255)
  13.     k$ = INKEY$
  14.     CLS
  15.     IF k$ = " " THEN
  16.         p(0).x = INT(RND * 600): p(0).y = INT(RND * 600)
  17.         p(1).x = INT(RND * 600): p(1).y = INT(RND * 600)
  18.         p(2).x = INT(RND * 600): p(2).y = INT(RND * 600)
  19.     END IF
  20.     m.x = _MOUSEX: m.y = _MOUSEY
  21.     IF insideTriangle(p(0), p(1), p(2), m) THEN COLOR red~& ELSE COLOR white~&
  22.     FOR i = 0 TO 2 'draw triangle
  23.         LINE (p(i).x, p(i).y)-(p((i + 1) MOD 3).x, p((i + 1) MOD 3).y)
  24.     NEXT
  25.     _DISPLAY
  26.     _LIMIT 60
  27.  
  28. 'p1,p2,p3 -> coordinates of triangle
  29. 'N is the test point.
  30. 'returns true if N is inside the triangle
  31. FUNCTION insideTriangle (p1 AS vec2, p2 AS vec2, p3 AS vec2, N AS vec2)
  32.     p_area& = triangleArea(p1, p2, N) + triangleArea(p1, N, p3) + triangleArea(N, p2, p3)
  33.     insideTriangle = (ABS(triangleArea(p1, p2, p3) - p_area&) <= 3)
  34. FUNCTION triangleArea& (p1 AS vec2, p2 AS vec2, p3 AS vec2)
  35.     triangleArea& = 0.5 * ABS(p1.x * (p2.y - p3.y) + p2.x * (p3.y - p1.y) + p3.x * (p1.y - p2.y))
  36.  
« Last Edit: June 20, 2020, 12:49:23 am by Ashish »
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #28 on: June 20, 2020, 12:08:14 am »
Ashish, you have made my day!!!

For those above who are terrified of vectors, this perfect solution contains the same calculation mine does, right down to the cross product of vectors --- except what Ashish just blessed us with is just so damn efficient and tight! A lovely lovely job. I think we'll all be answering to you as the QB64 president in a few short years.


« Last Edit: June 20, 2020, 12:10:50 am by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #29 on: June 20, 2020, 12:26:56 am »
Yeah well, looks like he only tested one triangle.

Oh that's clever though, the area of 3 little triangles with the point as a vertex has to equal the given triangle area.

To me that's just plain Geometry and no right hand rule in sight, LOL.
« Last Edit: June 20, 2020, 01:05:35 am by bplus »