## basic math in livecode

Stop by to discuss use cases, requirements, information architecture, flow diagraming, unit testing and usability.

Moderators: FourthWorld, Klaus

Samuele
Posts: 270
Joined: Mon Oct 11, 2021 7:05 pm
Location: Italy

### basic math in livecode

Hi, I'm using basic math in my script but i can't figure out something, first of all, is this the right place? second, I'm trying to determinate the slope of a created line with 2 points, from what i know the formula is: m= (Y1 - Y2) / (X1 - X2) and when the m is greater than 0 the line is like this: / , and when the m is negative the line is \.
in this script I've put this formula but for a reason that i don't know when i create a line like this: \ it says that the m is a positive number

Code: Select all

``````on Calculatem
#put all the single nnumbers of the points into variables to semplify the altogheter
put item 1 of sDragStartPosition into tsX
put item 2 of sDragStartPosition into tsY
put item 1 of sDragEndPosition into teX
put item 2 of sDragEndPosition into teY

#the function of the m (pendence)
put ((tsY - teY) / (tsX - teX)) into m

#Set the _X and the _Y as a further point according to the m and the teY / tsY
if (m > 0) and (teY < tsY)
then
put (teX + (sLineLength)) into _X
put CalculateFunction() into _Y
put _X  &  "," & _Y into sDragEndPosition
else
end if

end Calculatem
``````
any ideas?
Thanks!
Samuele.

dunbarx
VIP Livecode Opensource Backer
Posts: 8356
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

### Re: basic math in livecode

Samuele.

Depending on the values of your data, I get negative ratios.

Craig

dunbarx
VIP Livecode Opensource Backer
Posts: 8356
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

### Re: basic math in livecode

Try this on a new card. make a line graphic and a button. Put this in the button script:

Code: Select all

``````on mouseup
put the points of grc 1 into tPoints
put item 1 of line 2 of tPoints into tsX
put item 2 of of line 2 of tPoints into tsY
put item 1 of of line 1 of tPoints into teX
put item 2 of of line 1 of tPoints into teY

answer (tsY - teY) / (tsX - teX)
end mouseUp``````
You will get either a positive or negative slope depending on how you draw the line. Note that the answers are always of the wrong sign, if we "read" the line from left to right. Do you see why?

Craig

Samuele
Posts: 270
Joined: Mon Oct 11, 2021 7:05 pm
Location: Italy

### Re: basic math in livecode

dunbarx wrote:
Tue Mar 15, 2022 5:47 pm
Try this on a new card. make a line graphic and a button. Put this in the button script:

Code: Select all

``````on mouseup
put the points of grc 1 into tPoints
put item 1 of line 2 of tPoints into tsX
put item 2 of of line 2 of tPoints into tsY
put item 1 of of line 1 of tPoints into teX
put item 2 of of line 1 of tPoints into teY

answer (tsY - teY) / (tsX - teX)
end mouseUp``````
You will get either a positive or negative slope depending on how you draw the line. Note that the answers are always of the wrong sign, if we "read" the line from left to right. Do you see why?

Craig
Why is it like this?
i tried drawing either from from top to bottom and viceversa
it should be -1.74359 not +?!
Samuele.

Samuele
Posts: 270
Joined: Mon Oct 11, 2021 7:05 pm
Location: Italy

### Re: basic math in livecode

dunbarx wrote:
Tue Mar 15, 2022 5:34 pm
Samuele.

Depending on the values of your data, I get negative ratios.

Craig
the formula doesn't depend on the values, well if the values are from a / line they should be positive and if they come from a \ line they should be negative.
Am i right?
Samuele.

Samuele
Posts: 270
Joined: Mon Oct 11, 2021 7:05 pm
Location: Italy

### Re: basic math in livecode

I don't know if that's what you meant but i figured out something that you cannot apply the Cartesian plane rules to livecode unless you understand that livecode works the other way around meaning that the ordinate and abscissa start from the top left and not from the bottom left, so that makes things quite different.
P.S is there a way to change that? because it's really annoying and difficult to work with...
Last edited by Samuele on Tue Mar 15, 2022 9:40 pm, edited 1 time in total.
Samuele.

dunbarx
VIP Livecode Opensource Backer
Posts: 8356
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

### Re: basic math in livecode

Samuele.

Play with the attached stack. If you modify the line graphic and click the button, you can see the slope.]Step through the button handler. Let me know what you find and think.
Slopes.livecode.zip
Craig

EDIT. This one actually seems to work.
Last edited by dunbarx on Thu Mar 17, 2022 2:22 pm, edited 1 time in total.

dunbarx
VIP Livecode Opensource Backer
Posts: 8356
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

### Re: basic math in livecode

Samuele.

A line graphic in LC has two points.That is all.

Say you draw two similar line graphics, but start each from opposite ends, one going up to the left, and the other going down to the right. The points of those graphics will be "reversed", since LC lists the starting point from, well, the point you started from. Both graphics will appear identical, and both have the same slope. That is why you need to do the stuff in the button handler of the sample stack I sent.

There is no default basis of a Cartesian frame of reference, unless you create one from whole cloth on the card.

Did you play with the stack? Draw your own line graphic and watch the points in the field after clicking the button. Make sure there is only one line on the card, since the handler expects only one. But whichever way you draw, the slope will be correct as we think of it.

Craig
Last edited by dunbarx on Thu Mar 17, 2022 2:21 pm, edited 1 time in total.

richmond62
Livecode Opensource Backer
Posts: 7226
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

### Re: basic math in livecode

because it's really annoying and difficult to work with...
Personally I find it rather refreshing as it makes one have to be flexible
and think of things slightly differently.

stam
Posts: 1202
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

### Re: basic math in livecode

Samuele wrote:
Tue Mar 15, 2022 9:07 pm
ordinate and abscissa start from the top left and not from the bottom left, so that makes things quite different.
It's only "different" if you've never touched a programming language before.
This is a widely accepted convention across all programming languages, not a livecode specific thing.
Top Left is always 0,0, bottom right is max,max

dunbarx
VIP Livecode Opensource Backer
Posts: 8356
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

### Re: basic math in livecode

Stam.
Top Left is always 0,0, bottom right is max,max
Not sure what you mean. LC defines a line graphic based on the way it is drawn. See my post above. Line 1 of the points of such a graphic is the starting point, and line 2 of the points is where the mouse is released. There is no preferred or "standard" orientation; it is in fact arbitrary.

That is why the OP had trouble wrapping his head around the appearance of negative slopes in a "positive-going" line graphic. The points of such a graphic have no relationship to the orientation of that graphic. And the points are the only thing that matters.

Craig

FourthWorld
VIP Livecode Opensource Backer
Posts: 9102
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

### Re: basic math in livecode

stam wrote:
Thu Mar 17, 2022 4:03 pm
Samuele wrote:
Tue Mar 15, 2022 9:07 pm
ordinate and abscissa start from the top left and not from the bottom left, so that makes things quite different.
It's only "different" if you've never touched a programming language before.
This is a widely accepted convention across all programming languages, not a livecode specific thing.
Top Left is always 0,0, bottom right is max,max
In all fairness to Samuele and others new to programming, we're all new the beginning. And the arbitrary oddities of OSes and the software that runs on them is often counterintuitive.

While display devices used by software do indeed often require the 0,0 origin point be in the upper-left, we can appreciate that this is different from the only other context in which most folks encounter coordinate systems, math class.

From our earliest days in grade school we're trained to plot coordinates from bottom-left. That is, until we get into programming, where everything is literally upside down in this regard.

Complicating cognition further is that Turtle Graphics is how many are introduced to graphical algorithmic thinking, but there 0,0 is the in center of the workspace.

This unintuitive departure from Cartesia makes sense from the perspective of a device driver rasterizer, but how many newcomers are writing display drivers?

In a move as maddening as it might have seemed noble at the time, PostScript - one of the most popular rendering systems in use - has its 0,0 at bottom-left.

Of course, very few beyond those who spent a moment in the '90s sending PostScript instructions directly to their then-new laser printer to see how it works (guilty as charged) will ever hand-write that language. But that the designers of that system sought a return to Cartesian thinking reminds us how we were trained in youth, and how many other things in computing require us to think in novel ways to accommodate a machine ultimately too stupid to count past 1.

With LiveCode in particular, we hope to see more surprise as Samuele's. It is, after all, aiming to expand on the xTalk tradition of making programming available to experts in domains beyond programming.

Of course this still needs to be learned, OSes being how they are. But hopefully we can muster a little patience as we share that knowledge.
LiveCode development, training, and consulting services: Fourth World Systems

dunbarx
VIP Livecode Opensource Backer
Posts: 8356
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

### Re: basic math in livecode

Richard.

All good stuff.

But of course the "points", whether those of a line or any other kind of graphic, the loc of a control, whatever, are always taken from the topLeft.

That was not Samuele's issue. It was that those points do not directly lend themselves to Cartesian massaging, because there is an order in the way those points are listed in the property itself. This depends on how the line was drawn, and can be one of two ways. Same line, one of two different lists. That fact requires special treatment, to remove the ordering of the points list from the calculus when calculating thngs like the slope of the line.

Craig

SparkOut
Posts: 2635
Joined: Sun Sep 23, 2007 4:58 pm

### Re: basic math in livecode

It's definitely not a new subject!

viewtopic.php?p=8430#p8430