Page 1 of 1

basic math in livecode

Posted: Tue Mar 15, 2022 5:18 pm
by Samuele
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
      answer m
      put (teX + (sLineLength)) into _X
      put CalculateFunction() into _Y
      put _X  &  "," & _Y into sDragEndPosition
   else 
      answer m
   end if
   
end Calculatem
any ideas?
Thanks!

Re: basic math in livecode

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

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

Craig

Re: basic math in livecode

Posted: Tue Mar 15, 2022 5:47 pm
by dunbarx
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

Re: basic math in livecode

Posted: Tue Mar 15, 2022 6:18 pm
by Samuele
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?
Immagine 2022-03-15 181608.png
i tried drawing either from from top to bottom and viceversa
it should be -1.74359 not +?!

Re: basic math in livecode

Posted: Tue Mar 15, 2022 6:31 pm
by Samuele
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?

Re: basic math in livecode

Posted: Tue Mar 15, 2022 9:07 pm
by Samuele
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.
Webp.net-compress-image.jpg
P.S is there a way to change that? because it's really annoying and difficult to work with...

Re: basic math in livecode

Posted: Tue Mar 15, 2022 9:14 pm
by dunbarx
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
(1.15 KiB) Downloaded 119 times
Craig

EDIT. This one actually seems to work. :roll:

Re: basic math in livecode

Posted: Thu Mar 17, 2022 2:17 pm
by dunbarx
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

Re: basic math in livecode

Posted: Thu Mar 17, 2022 2:20 pm
by richmond62
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.

posting.php?mode=reply&f=35&t=36840

Re: basic math in livecode

Posted: Thu Mar 17, 2022 4:03 pm
by stam
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

Re: basic math in livecode

Posted: Thu Mar 17, 2022 4:22 pm
by dunbarx
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

Re: basic math in livecode

Posted: Thu Mar 17, 2022 5:04 pm
by FourthWorld
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.

Re: basic math in livecode

Posted: Thu Mar 17, 2022 5:42 pm
by dunbarx
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

Re: basic math in livecode

Posted: Thu Mar 17, 2022 7:41 pm
by SparkOut
It's definitely not a new subject!

viewtopic.php?p=8430#p8430