Page 1 of 2

Rotate a line?

Posted: Fri Jan 23, 2015 11:04 pm
by uelandbob
I have a line that is a radius vector in a circle. I want to rotate the line 30 degrees counter clockwise when I click on a button. Is that possible? (Rotate "angle" does not seem to work for a line graphics. Also I want to rotate around the end point of the line, like a upside-down pendulum.)

Re: Rotate a line?

Posted: Sat Jan 24, 2015 12:44 am
by Simon

Re: Rotate a line?

Posted: Sat Jan 24, 2015 12:59 am
by uelandbob
Thanks Simon :D ,

but it doesn't quite answer my question. There was a link there that didn't work and the Harmonic oscillator stack didn't have a line for it's pendulum. So does it mean seems that there is no simple solution?

Re: Rotate a line?

Posted: Sat Jan 24, 2015 1:04 am
by Simon
The link for that stack is under the image
"LiveCode Stack"
But there are a couple more simple versions in the previous link (3 pages)

Simon
Edit;
and the Harmonic oscillator stack didn't have a line for it's pendulum.
Sorry misread that.
I see a line in the Pendulum tab???

Re: Rotate a line?

Posted: Sat Jan 24, 2015 11:35 am
by uelandbob
Simon wrote: I see a line in the Pendulum tab???
Yes but it disappears as soon as you start the pendulum.

A rotation of a simple line shouldn't be rocket science, and the fact that LC has no support for it is somewhat dissapointing :(

Re: Rotate a line?

Posted: Sat Jan 24, 2015 11:44 am
by Simon
hmmm...
This doesn't work for you?
pendulum2.zip
(1.14 KiB) Downloaded 40 times
It's my sad attempt, not as good as the others.

Simon

Re: Rotate a line?

Posted: Sat Jan 24, 2015 2:34 pm
by richmond62
Play around with this :)
rotten_rotater.livecode.zip
(811 Bytes) Downloaded 46 times

Re: Rotate a line?

Posted: Sat Jan 24, 2015 2:43 pm
by Klaus
Blaming an IDE for not yet knowing how to do something is somewhat dissapointing. 8)

Re: Rotate a line?

Posted: Sat Jan 24, 2015 4:34 pm
by RogGuay
Here's a fresh copy of my Harmonic Oscillators stack:

https://www.dropbox.com/s/wwwrxqehi0173 ... e.zip?dl=0

The Pendulum Tab DOES show a pendulum WITH a line that does NOT disappear on my machine and many others. If your copy's pendulum line disappears, perhaps we can help you with that problem, first of all?

Cheers,

Roger

Re: Rotate a line?

Posted: Sat Jan 24, 2015 5:24 pm
by uelandbob
RogGuay wrote:a pendulum WITH a line that does NOT disappear on my machine and many others. If your copy's pendulum line disappears, perhaps we can help you with that problem, first of all?
Roger
Thanks Roger :D ,

You are right. On my machine it disappears and is turned into a point. I'm running version 7.0.0 Build 10018 on a OS X Yosemite version 10.10.1.

As soon as I start to drag the M ball the line (the graphics called PL) disappears, or rather is turned into a point.

Re: Rotate a line?

Posted: Sat Jan 24, 2015 5:45 pm
by Klaus
Hi Bob,

you should always work with the latest version which is 7.02 already. http://downloads.livecode.com/livecode/
Eventual bugs from older version may have been fixed in the meantime :D


Best

Klaus

Re: Rotate a line?

Posted: Sat Jan 24, 2015 9:37 pm
by uelandbob
Klaus wrote:latest version which is 7.02 already.
Thanks Klaus :D , I downloaded 7.02 and tried, but no luck the error persists

Re: Rotate a line?

Posted: Sat Jan 24, 2015 10:09 pm
by mattmaier
I just went through the process of trying to find livecode's built-in functions for manipulating geometry and trig and whatnot. They don't exist. Or, rather, there are a couple but they only work for graphics and they only use their "points" which have to be rounded off. After you work with text and buttons and stuff you kind of intuitively want to type "point the thing at the thing" and expect livecode to figure out how to rotate something to point at something else. But it doesn't, so you have to roll your own.

Here are some functions I wrote that solved my problem. They're kind of general so they should solve yours and if they don't they should be easy to reverse engineer. The basic idea is that you should keep the coordinate math separate from the existing controls and their points. When you're all done, round off your coordinates and set them as points.

rotateCoord
This function takes in a list of coordinates (like points but don't have to be whole numbers) and rotates them around their origin (0,0) by the same amount as the bearing (a list of two coordinates, which makes a line). You could convert it to just use a supplied angle instead of calculating it from the bearing.

Code: Select all

function translateCoord coordinates,newOrigin
   -- coordinates = a list of coordinates (x,y cr x,y cr x,y etc)
   -- newOrigin = a coordinate (x,y)
   repeat for each line tLine in coordinates
      -- translate along x axis
      put item 1 of tLine + item 1 of newOrigin into tNewX
      -- translate along y axis
      put item 2 of tLine + item 2 of newOrigin into tNewY
      put return & tNewX & "," & tNewY after tNewCoord
      filter tNewCoord without empty
   end repeat
   return tNewCoord
end translateCoord

function rotateCoord coordinates,bearing
   -- coordinates = list of coordinates (x,y cr x,y cr x,y etc)
   -- bearing = a list of two pairs of coordinates for orientation (x,y cr x,y)
   -- tested this, it's not the problem
   put (item 1 of line 2 of bearing) - (item 1 of line 1 of bearing) into tDistX
   put (item 2 of line 2 of bearing) - (item 2 of line 1 of bearing) into tDistY
   
   -- tested this, it's not the problem
   put atan2(tDistX,tDistY) into tBearingAngle
   
   repeat for each line tLine in coordinates
      -- calculate angle of coordinates
      -- tested this, it's not the problem
      put atan2(item 1 of tLine,item 2 of tLine) into tAngle
      -- calculate coordinates' distance from origin
      put sqrt((item 1 of tLine)^2 + (item 2 of tLine)^2) into tHypot
      -- calculate new x coordinate
      put tHypot * cos(tAngle - tBearingAngle) into tNewX
      -- calculate new y coordinate
      put tHypot * sin(tAngle - tBearingAngle) into tNewY      
      put return & tNewX & "," & tNewY after tNewCoord
   end repeat
   filter tNewCoord without empty
   return tNewCoord
end rotateCoord

function batchRoundCoord coordinates
   -- coordinates = a list of coordinates (x,y cr x,y cr x,y etc)
   repeat for each line tLine in coordinates
      if tLine is empty then
         put return after tNewCoord
      else
         -- round the number off so it can be a point
         put the round of item 1 of tLine into tNewX
         put the round of item 2 of tLine into tNewY
         put return & tNewX & "," & tNewY after tNewCoord
      end if
   end repeat
   return tNewCoord
end batchRoundCoord

function divideLine coordinates,distance
   -- coordinates = a list of two pairs of coordinates (x,y cr x,y)
   -- distance = maximum length of each subdivision
   put coordinates into tNewCoord
   -- calculate distance between coordinate pairs
   put (item 1 of line 2 of coordinates) - (item 1 of line 1 of coordinates) into tDistX
   put (item 2 of line 2 of coordinates) - (item 2 of line 1 of coordinates) into tDistY
   put sqrt(tDistX^2 + tDistY^2) into tHypot
   if distance < tHypot then
      -- divide distance in half
      put (tDistX / 2) + item 1 of line 1 of coordinates into tMidX
      put (tDistY / 2) + item 2 of line 1 of coordinates into tMidY
      -- coordinates for first half
      put line 1 of coordinates into tFirstCoord
      put return & tMidX & "," & tMidY after tFirstCoord
      -- coordinates for second half
      put tMidX & "," & tMidY into tNextCoord
      put return & line 2 of coordinates after tNextCoord
      -- call this function on each half
      put return & divideLine(tFirstCoord,distance) after tNewCoord
      put return & divideLine(tNextCoord,distance) after tNewCoord
   else
      return tNewCoord
   end if
   filter tNewCoord without empty
   sort tNewCoord ascending
   return tNewCoord
end divideLine

Re: Rotate a line?

Posted: Sat Jan 24, 2015 10:10 pm
by Simon
Hi Bob,
Yes, I'm getting the same in 7.
It works in 6.7

Simon

Re: Rotate a line?

Posted: Sat Jan 24, 2015 11:26 pm
by RogGuay
OK, I know what's wrong. LC V 7 requires that points of a line be handled differently than earlier versions. I will fix my Harmonic Oscillators stack and repost a link to it ASAP.

Thanks,

Roger