All Points in a Line

Visuals, audio, animation. Blended, not stirred. If LiveCode is part of your rich media production toolbox, this is the forum for you.

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
capellan
Posts: 610
Joined: Wed Aug 15, 2007 11:09 pm
Contact:

All Points in a Line

Post by capellan » Sat Jan 12, 2019 4:00 am

Hi All,

This stack shows one script that calculates
all coordinates points of a line.

You could move the endpoints of the line
and change line length and orientation.

I found this stack while backing up
my data from last year and surely
you will find a faster and shorter
script that produce the same result.

Add a new button and test
your own script.

Al
All_Points_in_Line.livecode.zip
Compressed stack
(4.12 KiB) Downloaded 42 times

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2123
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: All Points in a Line

Post by [-hh] » Sun Jan 13, 2019 1:51 am

This opens an interesting problem: Simply round your values to draw the pixels of your line (without antialiasing)?

But see here
viewtopic.php?p=137268#p137268

This uses the Bresenham algorithm for drawing lines (is NOT the same as rounding your points to draw the pixels of a line): https://en.wikipedia.org/wiki/Bresenham ... _algorithm
shiftLock happens

capellan
Posts: 610
Joined: Wed Aug 15, 2007 11:09 pm
Contact:

Re: All Points in a Line

Post by capellan » Sun Jan 13, 2019 7:58 pm

Bresenham's line algorithm is probably the best alternative
for this kind of work, but Does this work with antialiased
graphics? I have to test the accuracy of this and others
line drawing algorithms.

The original purpose of this script was to find all points
in a line and calculate the exact intersection points of
superposed graphics like the attached image that shows
20 intersection points.

Al
vector graphics superposed.png
Vector graphics superposed
Vector Graphics Intersection v01.livecode.zip
Compressed stack
(1.84 KiB) Downloaded 40 times
Booba-Kiki.svg.zip
Compressed svg file
(2.18 KiB) Downloaded 37 times

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2123
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: All Points in a Line

Post by [-hh] » Sun Jan 13, 2019 9:07 pm

Bresenham's algorithm is simply a method to choose the integer points for a line such that the sum of the absolute differences between "true" points and integer points is minimal.
This is today still important for small displays such as the screens in "house automation".

Your problem has nothing to do with antialiasing because such (sub-)pixels do NOT count to the points of a curve/line. And it has to do with the Bresenham algorithm only if the LC/OS-display uses it.

The most abstract model for your problem is to get the intersections of two SVG path objects. This is done in the SVG libraries by "segmenting" the paths, that is they are approximating these by very small linear segments (polylines) and then looking for the intersections of these small line segments.

Obviously this becomes very slow if you need high accuracy. I doubt that LC Script is fast enough (for more than "hello path" examples) and LC Builder has no fast method from a SVG library available for that.

But you can do it by using a JavaScript library (in a browser widget),
see the example here:
http://paperjs.org/examples/path-intersections/
and here (where you can set the number of segments, use "Show segments" there):
https://bl.ocks.org/bricof/f1f5b4d4bc02 ... a3c5ff8ad7
shiftLock happens

capellan
Posts: 610
Joined: Wed Aug 15, 2007 11:09 pm
Contact:

Re: All Points in a Line

Post by capellan » Mon Jan 14, 2019 3:47 am

Both webpages are very useful!
Thanks a lot Hermann! :D

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2123
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: All Points in a Line

Post by [-hh] » Tue Jan 15, 2019 10:33 pm

Here is a stack that does with an example what I suggested (using a svg library with the javaScript of a browser widget). I used svg.js ( https://svgjs.com ), a library that I wanted to test anyway and two of the plugins there.

All needed javascript is included in the stack. You can drag the two lines around in the browser window and see the intersection points drawn with mouseMove and also listed in fld "OUT" with every mouseUp.

You can intersect
= lines vs. lines
= lines vs. paths
= paths vs. paths

For inserting your own paths it is probably best to copy the LC field in a text editor that has javascript colorizing.
If you have N objects you need N*(N-1)/2 comparisons and compute the corresponding intersections. So the needed time will go up quadratic with N, even the GPU accelerated javaScript will become slow if N increases.

Have fun!

There is also an animation example in the stack (that was the reason for my tests of svg.js, I like it).
Acknowledgement: My two examples are (enhanced) examples of svg.js (or plugins).
 
Attachments
svgJS2.livecode.zip
(48.65 KiB) Downloaded 45 times
svgJS.png
Drag the lines with the mouse and see the intersections updating.
shiftLock happens

capellan
Posts: 610
Joined: Wed Aug 15, 2007 11:09 pm
Contact:

Re: All Points in a Line

Post by capellan » Wed Jan 16, 2019 12:13 am

Amazing! Thanks a lot for making this stack. :D

By the way, given that you are using the default
browser for running Javascript, Have you benchmarked
the Apple javascript engine of the default browser
(JavascriptCore) against the standalone engines
Google V8 and Firefox Spidermonkey?

https://v8.dev/?csw=1
http://macappstore.org/spidermonkey
https://developer.apple.com/documentati ... scriptcore

Al

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2123
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: All Points in a Line

Post by [-hh] » Wed Jan 16, 2019 12:34 am

What would you do with such "standalone" engines? Attach to a LC stack?
It would be better if LC would provide a "pure" JS access to libbrowser (without native display).

If I wish to have real speed I use webgl. But I refuse to publish apps/stacks that run in very special browsers and with very special hardware only.
shiftLock happens

capellan
Posts: 610
Joined: Wed Aug 15, 2007 11:09 pm
Contact:

Re: All Points in a Line

Post by capellan » Wed Jan 16, 2019 1:34 am

[-hh] wrote:
Wed Jan 16, 2019 12:34 am
What would you do with such "standalone" engines? Attach to a LC stack?
It would be better if LC would provide a "pure" JS access to libbrowser (without native display).

If I wish to have real speed I use webgl. But I refuse to publish apps/stacks that run in very special browsers and with very special hardware only.
Just like Thierry have made the perl engine available from LiveCode,
maybe in a near future, python, lua and javascript are available too.

https://sunny-tdz.com/livecode/sunnyperl

Al

Post Reply

Return to “Rich Media”