Page 1 of 1

Lines Crossing

Posted: Tue Sep 25, 2018 6:55 pm
by richmond62
on mouseUp
put item 1 of the topleft of grc "g1" into X1
put "x1 = " & X1 into line 1 of fld "fPOINTS"
----------
put item 2 of the topleft of grc "g1" into Y1
put "y1 = " & Y1 into line 2 of fld "fPOINTS"
----------
put item 1 of the bottomright of grc "g1" into X2
put "x2 = " & X2 into line 3 of fld "fPOINTS"
----------
put item 2 of the bottomright of grc "g1" into Y2
put "y2 = " & Y2 into line 4 of fld "fPOINTS"
----------
----------
put item 1 of the bottomleft of grc "g2" into X3
put "x3 = " & X3 into line 5 of fld "fPOINTS"
----------
put item 2 of the bottomleft of grc "g2" into Y3
put "y3 = " & Y3 into line 6 of fld "fPOINTS"
----------
put item 1 of the topright of grc "g2" into X4
put "x4 = " & X4 into line 7 of fld "fPOINTS"
----------
put item 2 of the topright of grc "g2" into Y4
put "y4 = " & Y4 into line 8 of fld "fPOINTS"
----------
-- Heavy Lifting Starts Here
----------
put (((X1*Y2)-(Y1*X2))*(X3-X4)) into P1
put (((X3*Y4)-(Y3*X4))*(X1-X2)) into P2
put ((X1-X2)*(Y3-Y4)) into P3
put ((Y1-Y2)*(X3-X4)) into P4
put (P1-P2)/(P3-P4) into XX
--------------
put (((X1*Y2)-(Y1*X2))*(Y3-Y4)) into Q1
put (((X3*Y4)-(Y3*X4))*(Y1-Y2)) into Q2
put (Q1-Q2)/(P3-P4) into YY
--------------
put "Lines intersect at " & XX & "," & YY into fld "OOT"
end mouseUp
Intersection.png
INTERSECTIONS1.livecode.zip
Here's the stack.
(1.59 KiB) Downloaded 253 times

Re: Lines Crossing

Posted: Tue Sep 25, 2018 6:58 pm
by richmond62
With wiggly lines things get nasty as one has to determine which 2 line segments cross each other before performing that sort of operation.

Re: Lines Crossing

Posted: Tue Sep 25, 2018 7:52 pm
by dunbarx
Richmond.

Mostly good. But go back to the thread that started all this. When you do the math, like you did, you get points to six decimal places, assuming you do not touch the numberFormat. You must, and it is a little thing to do, find the nearest intersecting pixel.

Pixels are fixed, er, points on screen. Points are mathematical objects. In order to use them later, unless this is just a math exercise instead of a programming one, you need to dumb down your points. :wink:

Craig

Re: Lines Crossing

Posted: Tue Sep 25, 2018 8:11 pm
by richmond62
you need to dumb down your points
Cripes, I thought the chunky sweater I was wearing stopped them showing. 8)

Re: Lines Crossing

Posted: Wed Sep 26, 2018 5:48 pm
by jacque
If I remember right, the last time I had to deal with fractional points the engine helpfully rounded them for me. Worth testing.

Re: Lines Crossing

Posted: Wed Sep 26, 2018 7:03 pm
by dunbarx
the engine helpfully rounded them for me. Worth testing.
You know, I seem to remember this.

And it does indeed round to the nearest whole number. Convenient; likely little downside.

Craig

Re: Lines Crossing

Posted: Thu Sep 27, 2018 9:27 pm
by capellan
Hi All,

For beginners that are reading this forum thread,
I just want to remember you something obvious
for anyone who had worked before
with vector graphics:

The actual graphic, (the real graphic) is made of
coordinates with large decimal points. Coordinates
like these:

371.142785,164.376481
35.274689,287.410245

The graphics that you see on the screen, are draw
using plain coordinates like these:

371,164
35,287

When you apply 2D Transformation to vector graphics,
math operations like scale, rotation, translation, shearing,
reflection, perspective, etc. always, always, always
create, use and store coordinates with large decimal points,
like these:

371.142785,164.376481
35.274689,287.410245

For example, if you receive these coordinates:
371,164
35,287

convert them to:
371.000000,164.000000
35.000000,287.000000

using the numberformat function...

Code: Select all

put 371 into x1
set the numberformat to "#.000000"
put x1 * 1 into x1
put x1 -- x1 now contains 371.000000
https://www.tutorialspoint.com/computer ... mation.htm