Calculating y relative to x

Want to move your code and projects to LiveCode but don't know where to start?

Moderators: FourthWorld, heatherlaine, Klaus, robinmiller

Post Reply
calmrr3
Posts: 100
Joined: Mon Oct 28, 2013 2:39 pm

Calculating y relative to x

Post by calmrr3 » Sun Jan 18, 2015 6:59 pm

Hello,

I might not have posted this in the right section but I couldn't see a section for general code questions.

So the problem I am having is this:

I have a number of gps coordinates which I am plotting, I am able to fit the plotted route to within the card width (320px) using:

Code: Select all

NewValue = (((OldValue - OldMin) * (NewMax - NewMin)) / (OldMax - OldMin)) + NewMin
which translates into:

Code: Select all

put (((xLoc - tMin) * (320-0)) / (tMax - tMin)) + 0 into xLocNew
The problem is, when I try and plot the longitude coordinates I can't work out a way to scale them relative to the remapped latitude values.


If I use the same method above and remap the maximum and minimum longitude coordinates to the height of the card(480px) and 0 then the plotted gps route will look stretched.

I thought about remapping them to the same scale (0 -320) as the latitude values which gives a better result but I still don't think it is correct.

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

Re: Calculating y relative to x

Post by SparkOut » Sun Jan 18, 2015 11:40 pm

You need to calculate the ratio of both the range of x values to plot compared to the width of the display area, and the ratio of the range of y values to plot compared to the height. Choose the smaller of the two ratios and use that for the multiplier for the plot values of BOTH sets of coordinates.
That will maximise the scale of the plotted course but without distorting.

calmrr3
Posts: 100
Joined: Mon Oct 28, 2013 2:39 pm

Re: Calculating y relative to x

Post by calmrr3 » Mon Jan 19, 2015 11:19 am

So in the case of this card, where it has a width of 320 and height of 480, The multiplier would be 320?

Like this:

Code: Select all

put (((OldXCoord - OldMaximumXCoord) * (NewMaximumXCoord - NewMinimumXCoord)) / (OldMaximumXCoord - OldMinimumXCoord)) + NewMinimumXCoord into xLocNew

becomes:

put (((OldXCoord - OldMaximumXCoord) * (320 - 1)) / (OldMaximumXCoord - OldMinimumXCoord)) + 1 into xLocNew

put (((OldYCoord - OldMaximumYCoord) * (320 - 1)) / (OldMaximumYCoord - OldMinimumYCoord)) + 1 into yLocNew
?

Thanks

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

Re: Calculating y relative to x

Post by SparkOut » Mon Jan 19, 2015 3:46 pm

Well I'm not sure what your data is and how you have been constructing the plot and how you want to make it look, but as an example:

Code: Select all

--get the range of x values to plot
--I'm not sure where you get them, but it would be something like

put field "theXCoordinatesList" into tXlist

--if comma delimited, you can:
--put min(tXlist) into tXmin
--put max(tXlist) into tXmax

--otherwise, if cr delimited (one value per line):
put tXlist into tXsorted
sort tXsorted numeric ascending
put line 1 of tXsorted into tXmin
put the last line of tXsorted into tXmax

put tXmax - tXmin into tXrange

--do the same for the range of y values to plot
put field "theYCoordinatesList" into tYlist

--if comma delimited, you could:
--put min(tYlist) into tYmin
--put max(tYlist) into tYmax

--otherwise, if cr delimited (one value per line):
put tYlist into tYsorted
sort tYsorted numeric ascending
put line 1 of tYsorted into tYmin
put the last line of tYsorted into tYmax

put tYmax - tYmin into tYrange

--compare the range of x values to the width of the display area
--(be prepared for this to change on different mobile hardware or resizeable stack)
--for now it's hard coded as 320
put 320 into tDisplayWidth
put tDisplayWidth / tXrange into tXratio

--do the same to compare the range of y values to the height of the display area
--(be prepared for this to change on different mobile hardware or resizeable stack)
--for now it's hard coded as 480
put 480 into tDisplayHeight
put tDisplayHeight / tYrange into tYratio

--to keep the whole plot on the screen but keep the dimensions in the same proportion
--we choose the smallest ratio
put min(tXratio,tYratio) into tRatio

--plot the points (using the unsorted lists), in this case using a line to draw between points on a course
--if it was a comma delimited list then substitute "line" for "item" in the line below

repeat for each line tXcoord in tXlist
   --take off the minimum value as we're using that as the origin for the plot
   --so the minimum now becomes zero
   put (tXcoord - tXmin) * tRatio + 1 into tNewX
   put tNewX & cr after tNewXlist
end repeat

repeat for each line tYcoord in tYlist
   --take off the minimum value as we're using that as the origin for the plot
   --so the minimum now becomes zero
   put (tYcoord - tYmin) * tRatio + 1 into tNewY
   --but Y coordinates plot from the top of the card so turn it upside down
   --so that the course has the lowest y value at the bottom of the card
   put tDisplayHeight - tNewY into tNewY
   put tNewY & cr after tNewYlist
end repeat

repeat with i = 1 to the number of lines of tNewXlist
  --you have a problem if the number of lines of tNewYlist doesn't match!
  put line i of tNewXlist & comma & line i of tNewYlist & cr after tPlotPoints
end repeat

set the points of graphic "grcCoursePlot" to tPlotPoints
set the visible of graphic "grcCoursePlot" to true
There's a sample stack attached with this code crudely implemented to demonstrate the principle
Attachments
coordinates_plot.zip
(1.97 KiB) Downloaded 455 times

Post Reply

Return to “Converting to LiveCode”