All Points in a Line
Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller
All Points in a Line
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
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

 VIP Livecode Opensource Backer
 Posts: 1956
 Joined: Thu Feb 28, 2013 11:52 pm
 Location: Göttingen, DE
Re: All Points in a Line
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
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
Re: All Points in a Line
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
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

 VIP Livecode Opensource Backer
 Posts: 1956
 Joined: Thu Feb 28, 2013 11:52 pm
 Location: Göttingen, DE
Re: All Points in a Line
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/OSdisplay 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/pathintersections/
and here (where you can set the number of segments, use "Show segments" there):
https://bl.ocks.org/bricof/f1f5b4d4bc02 ... a3c5ff8ad7
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/OSdisplay 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/pathintersections/
and here (where you can set the number of segments, use "Show segments" there):
https://bl.ocks.org/bricof/f1f5b4d4bc02 ... a3c5ff8ad7
shiftLock happens
Re: All Points in a Line
Both webpages are very useful!
Thanks a lot Hermann!
Thanks a lot Hermann!

 VIP Livecode Opensource Backer
 Posts: 1956
 Joined: Thu Feb 28, 2013 11:52 pm
 Location: Göttingen, DE
Re: All Points in a Line
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*(N1)/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).
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*(N1)/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 25 times
shiftLock happens
Re: All Points in a Line
Amazing! Thanks a lot for making this stack.
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
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

 VIP Livecode Opensource Backer
 Posts: 1956
 Joined: Thu Feb 28, 2013 11:52 pm
 Location: Göttingen, DE
Re: All Points in a Line
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.
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
Re: All Points in a Line
Just like Thierry have made the perl engine available from LiveCode,[hh] wrote: ↑Wed Jan 16, 2019 12:34 amWhat 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.
maybe in a near future, python, lua and javascript are available too.
https://sunnytdz.com/livecode/sunnyperl
Al