## Rotate a line?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

uelandbob
Posts: 72
Joined: Mon Dec 29, 2014 3:28 pm

### Rotate a line?

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.)
uelandbob@gmail.com

Simon
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

### Re: Rotate a line?

I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

uelandbob
Posts: 72
Joined: Mon Dec 29, 2014 3:28 pm

### Re: Rotate a line?

Thanks Simon ,

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?
uelandbob@gmail.com

Simon
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

### Re: Rotate a line?

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???
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

uelandbob
Posts: 72
Joined: Mon Dec 29, 2014 3:28 pm

### Re: Rotate a line?

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
uelandbob@gmail.com

Simon
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

### Re: Rotate a line?

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

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

### Re: Rotate a line?

Play around with this
rotten_rotater.livecode.zip
(811 Bytes) Downloaded 45 times

Klaus
Posts: 11579
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

### Re: Rotate a line?

Blaming an IDE for not yet knowing how to do something is somewhat dissapointing.

RogGuay
VIP Livecode Opensource Backer
Posts: 67
Joined: Fri Apr 28, 2006 12:10 am

### Re: Rotate a line?

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

uelandbob
Posts: 72
Joined: Mon Dec 29, 2014 3:28 pm

### Re: Rotate a line?

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 ,

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.
uelandbob@gmail.com

Klaus
Posts: 11579
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

### Re: Rotate a line?

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

Best

Klaus

uelandbob
Posts: 72
Joined: Mon Dec 29, 2014 3:28 pm

### Re: Rotate a line?

Klaus wrote:latest version which is 7.02 already.
Thanks Klaus , I downloaded 7.02 and tried, but no luck the error persists
uelandbob@gmail.com

mattmaier
Posts: 109
Joined: Fri Apr 19, 2013 2:49 am

### Re: Rotate a line?

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``````

Simon
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

### Re: Rotate a line?

Hi Bob,
Yes, I'm getting the same in 7.
It works in 6.7

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

RogGuay
VIP Livecode Opensource Backer
Posts: 67
Joined: Fri Apr 28, 2006 12:10 am

### Re: Rotate a line?

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