Page 1 of 1

All Points in a Line

Posted: Sat Jan 12, 2019 4:00 am
by capellan
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

Re: All Points in a Line

Posted: Sun Jan 13, 2019 1:51 am
by [-hh]
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

Re: All Points in a Line

Posted: Sun Jan 13, 2019 7:58 pm
by capellan
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 Intersection v01.livecode.zip
Booba-Kiki.svg.zip

Re: All Points in a Line

Posted: Sun Jan 13, 2019 9:07 pm
by [-hh]
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

Re: All Points in a Line

Posted: Mon Jan 14, 2019 3:47 am
by capellan
Both webpages are very useful!
Thanks a lot Hermann! :D

Re: All Points in a Line

Posted: Tue Jan 15, 2019 10:33 pm
by [-hh]
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).
 

Re: All Points in a Line

Posted: Wed Jan 16, 2019 12:13 am
by capellan
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

Re: All Points in a Line

Posted: Wed Jan 16, 2019 12:34 am
by [-hh]
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.

Re: All Points in a Line

Posted: Wed Jan 16, 2019 1:34 am
by capellan
[-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