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

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #30 on: June 20, 2020, 12:49:37 am »
ouch! I updated the code @bplus
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 bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #31 on: June 20, 2020, 12:56:48 am »
ouch! I updated the code @bplus

LOL thanks, I did say it was clever ;-))

I got mine working now with or without a WINDOW statement that completely flips the axis, the only code I have to change is the mouse with PMAP so you can check any point you want, the triangles change rather quickly.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3. WINDOW (0, 600)-(800, 0) 'lets screw the hell up with the coodinate system
  4.  
  5. ' works with or without flopping the coordinate system with WINDOW statement above
  6.  
  7. 1 x1 = RND * 800: y1 = RND * 600: x2 = RND * 800: y2 = RND * 600: x3 = RND * 800: y3 = RND * 600
  8. WHILE _KEYDOWN(27) = 0
  9.     CLS: lc = lc + 1
  10.     IF lc >= 100 THEN lc = 1: GOTO 1 'new triangle
  11.  
  12.  
  13.     'use this with WINDOW command
  14.     WHILE _MOUSEINPUT: WEND: px = PMAP(_MOUSEX, 0): py = PMAP(_MOUSEY, 1)
  15.  
  16.     'use this without Window
  17.     'WHILE _MOUSEINPUT: WEND: px = _MOUSEX: py = _MOUSEY
  18.  
  19.  
  20.     tf = PtInteriorOfTriangleTF(x1, y1, x2, y2, x3, y3, px, py)
  21.     IF tf THEN PRINT "Pt ("; px; ","; py; ") in or on." ELSE PRINT "Pt ("; px; ","; py; ") is Out."
  22.     IF INKEY$ = " " THEN
  23.         SLEEP
  24.     END IF
  25.     _DISPLAY: _LIMIT 30
  26.  
  27. SUB drawSituation (tx1, ty1, tx2, ty2, tx3, ty3, px, py) '3 points of triangle and test pt yellow
  28.     LINE (tx1, ty1)-(tx2, ty2)
  29.     LINE (tx2, ty2)-(tx3, ty3)
  30.     LINE (tx3, ty3)-(tx1, ty1)
  31.     CIRCLE (px, py), 3, &HFFFFFF00
  32.     CIRCLE (tx1, ty1), 3, &HFFFF0000
  33.     CIRCLE (tx2, ty2), 3, &HFF00AA00
  34.     CIRCLE (tx3, ty3), 3, &HFF0000FF
  35.  
  36. FUNCTION PtInteriorOfTriangleTF (tx1, ty1, tx2, ty2, tx3, ty3, px, py)
  37.     drawSituation tx1, ty1, tx2, ty2, tx3, ty3, px, py
  38.     IF PtInteriorOfAngleTF(tx1, ty1, tx2, ty2, tx3, ty3, px, py) THEN
  39.         IF PtInteriorOfAngleTF(tx2, ty2, tx1, ty1, tx3, ty3, px, py) THEN
  40.             IF PtInteriorOfAngleTF(tx3, ty3, tx1, ty1, tx2, ty2, px, py) THEN
  41.                 PtInteriorOfTriangleTF = -1
  42.             END IF
  43.         END IF
  44.     END IF
  45.  
  46. FUNCTION PtInteriorOfAngleTF (ox1, oy1, ax1, ay1, ax2, ay2, ptx, pty)
  47.     a1 = atan360(_ATAN2(ay1 - oy1, ax1 - ox1)) ' angle of one arm from origin of angle
  48.     a2 = atan360(_ATAN2(ay2 - oy1, ax2 - ox1)) ' angle of the other arm from origin of angle
  49.     ap = atan360(_ATAN2(pty - oy1, ptx - ox1)) ' angle of point from same origin
  50.     IF a2 < a1 THEN SWAP a2, a1 'make a1 the samller of the two angles for test fit of point
  51.     IF ABS(a2 - a1) > 180 THEN 'can't be so we are crossing the point where angles go from 360 to 0
  52.         a1Fix = a1 + 360 'fix  a1Fix is now the greater angle
  53.         IF ap < a1 THEN apFix = ap + 360 ELSE apFix = ap 'a1Fix is now the greater angle
  54.         IF apFix <= a1Fix AND apFix >= a2 THEN PtInteriorOfAngleTF = -1
  55.     ELSE
  56.         IF ap >= a1 AND ap <= a2 THEN PtInteriorOfAngleTF = -1
  57.     END IF
  58.  
  59.     IF ap >= a1 AND ap <= a2 THEN PtInteriorOfAngleTF = -1 ' is inside or on the 2 arm angles
  60.     PRINT a1, a2, ap, PtInteriorOfAngleTF
  61.  
  62. FUNCTION atan360 (ra) 'convert radian angle from _ATAN2()  to guaranteed positive angle
  63.     atan360 = _R2D(ra + _PI(2)): IF atan360 > 359.9999 THEN atan360 = atan360 - 360
  64.  
  65.  

 

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #32 on: June 20, 2020, 01:02:47 am »
Yep! Ashish has a winner!

Here is a tighter translation:
Code: QB64: [Select]
  1. _TITLE "Translate Ashish Interior Triangle to less code"
  2. SCREEN _NEWIMAGE(600, 600, 32)
  3. red~& = _RGB(255, 0, 0): white~& = _RGB32(255)
  4. 1 x1 = INT(RND * 600): y1 = INT(RND * 600): x2 = INT(RND * 600): y2 = INT(RND * 600): x3 = INT(RND * 600): y3 = INT(RND * 600)
  5.     k$ = INKEY$
  6.     CLS
  7.     IF k$ = " " THEN GOTO 1
  8.     mx = _MOUSEX: my = _MOUSEY
  9.     IF insideTriangle(x1, y1, x2, y2, x3, y3, mx, my) THEN COLOR red~& ELSE COLOR white~&
  10.     LINE (x1, y1)-(x2, y2): LINE (x2, y2)-(x3, y3): LINE (x3, y3)-(x1, y1)
  11.     _DISPLAY
  12.     _LIMIT 60
  13. FUNCTION insideTriangle (x1, y1, x2, y2, x3, y3, px, py)
  14.     area& = triangleArea(x1, y1, x2, y2, px, py) + triangleArea(x1, y1, px, py, x3, y3) + triangleArea(px, py, x2, y2, x3, y3)
  15.     insideTriangle = (ABS(triangleArea(x1, y1, x2, y2, x3, y3) - area&) <= 3)
  16. FUNCTION triangleArea& (x1, y1, x2, y2, x3, y3)
  17.     triangleArea& = 0.5 * ABS(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))
  18.  
« Last Edit: June 20, 2020, 01:21:31 am by bplus »

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #33 on: June 20, 2020, 02:24:19 am »
@bplus you can reduce the lines even more! ;)
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 luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #34 on: June 20, 2020, 06:43:25 am »
It looks like you can cut out those pesky constants, too:
Code: [Select]
FUNCTION insideTriangle (x1, y1, x2, y2, x3, y3, px, py)
    area& = triangleArea2(x1, y1, x2, y2, px, py) + triangleArea2(x1, y1, px, py, x3, y3) + triangleArea2(px, py, x2, y2, x3, y3)
    insideTriangle = area& <= triangleArea2(x1, y1, x2, y2, x3, y3)
END FUNCTION
FUNCTION triangleArea2& (x1, y1, x2, y2, x3, y3)
    triangleArea2& = ABS(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))
END FUNCTION

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #35 on: June 20, 2020, 07:15:16 am »
To me that's just plain Geometry and no right hand rule in sight, LOL.

Still nope.

EDIT

Yeah blah blah blah the third vector is perpendicular to the originals and doesn't count anymore. That's not true. The right hand rule manifests in the 2D plane as a NEGATIVE SIGN. Right? Because if A and B are vectors, you should remember that:

A x B = - B x A

in any coordinates. The fact that the sign changes when you swap the arguments IS the right hand rule in two dimensions. Yes, you don't have to deal with the entire third dimension, but you're constant trying not to step in its shadow with those negative signs.

I see what you want to mean in all this, but the important thing is you see what I mean, lol.
« Last Edit: June 20, 2020, 07:40:23 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 #36 on: June 20, 2020, 11:15:36 am »
Still nope.

EDIT

Yeah blah blah blah the third vector is perpendicular to the originals and doesn't count anymore. That's not true. The right hand rule manifests in the 2D plane as a NEGATIVE SIGN. Right? Because if A and B are vectors, you should remember that:

A x B = - B x A

in any coordinates. The fact that the sign changes when you swap the arguments IS the right hand rule in two dimensions. Yes, you don't have to deal with the entire third dimension, but you're constant trying not to step in its shadow with those negative signs.

I see what you want to mean in all this, but the important thing is you see what I mean, lol.

I know you know vector math way better than I, thanks for showing it in simple enough form that even I could understand in 2D. :) 

Not using vector math, I never noticed having to swap signs. I do know if I use WINDOW and make y increase going up, all the trig calculations are fine without change and as the angle increases the direction reverses visually from clockwise to counter-clockwise, again with no change in math.

So you prefer to work with coordinate systems completely independent of the screen, your own world so to speak.
Man, I confess I couldn't easily come up with WINDOW coordinates that would just tilt a 2D plane with WINDOW statement.
« Last Edit: June 20, 2020, 11:34:14 am by bplus »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #37 on: June 20, 2020, 11:22:21 am »
Yyyyyyikes, I hate the way I come off pretty much all the time. Thanks for hanging in there.

I thought you were just being extremely cheeky for its own sake, it didn't occur to me to make the argument as such until the end.
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 #38 on: June 20, 2020, 11:46:18 am »
Area of a Triangle says Luke:
Code: QB64: [Select]
  1. FUNCTION triangleArea2& (x1, y1, x2, y2, x3, y3)
  2.     triangleArea2& = ABS(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))
  3.  


Area of Triangle says Ashish:
Code: QB64: [Select]
  1. FUNCTION triangleArea& (x1, y1, x2, y2, x3, y3)
  2.     triangleArea& = 0.5 * ABS(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))
  3.  

I know with Ashish code above, it doesn't matter if area = .5 * (....   or 1000 * (... or _PI * (...
because you are using same calc to compare two areas but

I saved Ashish Area Triangle so I might use in other apps for real area in pixels and it might screw things up if I have wrong formula. So I guess I better test it. :) I think I have always used Hero's formula but you have to calc the 3 distances between points, so that will be judge.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #39 on: June 20, 2020, 11:50:49 am »
Yyyyyyikes, I hate the way I come off pretty much all the time. Thanks for hanging in there.

I thought you were just being extremely cheeky for its own sake, it didn't occur to me to make the argument as such until the end.

You are a working stiff still chasing after a paycheck, I am surprised you have time for any of this. I didn't when I was working. I tried and burned the candle at both ends and got crabby as hell and started interpreting people's intentions poorly, still not the greatest at that.

cheeky def: impudent or irreverent, typically in an endearing or amusing way.
Yeah OK, maybe some of that too :)
« Last Edit: June 20, 2020, 12:00:37 pm by bplus »

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #40 on: June 20, 2020, 12:51:57 pm »
cheeky def: impudent or irreverent, typically in an endearing or amusing way.
Every member here is tremendously endearing.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Harder-than-it-looks math problem
« Reply #41 on: June 20, 2020, 01:36:03 pm »
And Ashish formula for Area of Triangle was accurate even with triangles crossing over the axii:

Code: QB64: [Select]
  1. _TITLE "Area of a triangle : spacebar for next triangle, q to quit" 'b+ 2020-06-20
  2.  
  3. 'compare the functions and you might guess why I might prefer AshishArea for Area of a triangle given 3 points.
  4. ' But does it work in all cases of triangle coordinates? Yes!
  5.  
  6. ' I will use WINDOW to test cross axis triangles to see if negative and positive coordinates effect accuracy or break function.
  7.  
  8. SCREEN _NEWIMAGE(601, 601, 32)
  9. WINDOW (-100, 100)-(100, -100) 'cheeky, sarcastic, fun loving > our beloved mathematically correct graph
  10.  
  11. 1 x1 = RND * 200 - 100: y1 = RND * 200 - 100: x2 = RND * 200 - 100
  12. y2 = RND * 200 - 100: x3 = RND * 200 - 100: y3 = RND * 200 - 100
  13.     CLS
  14.     plus100
  15.     PRINT "("; x1 \ 1; ","; y1 \ 1; "), ("; x2 \ 1; ","; y2 \ 1; "), ("; x3 \ 1; ","; y3 \ 1; ")"
  16.     PRINT HeroArea(x1, y1, x2, y2, x3, y3), AshishArea(x1, y1, x2, y2, x3, y3)
  17.     LINE (x1, y1)-(x2, y2): LINE (x2, y2)-(x3, y3): LINE (x3, y3)-(x1, y1)
  18.     _DISPLAY
  19.     _LIMIT 30
  20.     k$ = INKEY$
  21.     IF k$ = "q" THEN SYSTEM
  22.     IF k$ = " " THEN GOTO 1
  23.  
  24. FUNCTION HeroArea (x1, y1, x2, y2, x3, y3)
  25.     a = _HYPOT(x1 - x2, y1 - y2)
  26.     b = _HYPOT(x2 - x3, y2 - y3)
  27.     c = _HYPOT(x3 - x1, y3 - y1)
  28.     s = (a + b + c) / 2
  29.     HeroArea = SQR(s * (s - a) * (s - b) * (s - c))
  30.  
  31. FUNCTION AshishArea (x1, y1, x2, y2, x3, y3)
  32.     'this is a keeper thanks Ashish
  33.     'FUNCTION triangleArea& (x1, y1, x2, y2, x3, y3)
  34.     AshishArea = 0.5 * ABS(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))
  35.  
  36. SUB plus100 'for bplus new standard math graph
  37.     FOR i = -100 TO 100: PSET (0, i): PSET (i, 0): NEXT
  38.  

and can we see enough WINDOW graphs and get enough practice with it? (dang cheeky again)

SCREEN _NEWIMAGE(601, 601, 32)  601??? maybe 600 x 600 will be OK

I had a hard time staying in bounds for this:
1 x1 = RND * 200 - 100: y1 = RND * 200 - 100: x2 = RND * 200 - 100
y2 = RND * 200 - 100: x3 = RND * 200 - 100: y3 = RND * 200 - 100

I was messing around with 201's because that is pixels across but dump INT() of x, y's and no fiddle with 1 less when mult by RND so 200 is fine. (Just some notes about using WINDOW, maybe only to myself.)

Maybe after getting use to origin in the middle, I will be ready to take on drawing vectors.
« Last Edit: June 20, 2020, 01:44:25 pm by bplus »