How do you set the angle of a line graphic?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10339
Joined: Wed May 06, 2009 2:28 pm

How do you set the angle of a line graphic?

Post by dunbarx » Thu Nov 12, 2020 10:05 pm

Am I missing something that simple? One can set the angle of a polygon, but not a line.

Or can one?

Or can one make an angle-settable graphic into something that looks like a line?

Craig

stam
Posts: 3092
Joined: Sun Jun 04, 2006 9:39 pm

Re: How do you set the angle of a line graphic?

Post by stam » Fri Nov 20, 2020 6:04 pm

Hey Craig,
Not sure i'll be of any help... but are you referring to a polygon shape and so on?
A 'line' can't have an angle by definition (it's just 2 points, you need 3 for an angle), unless i'm greatly misunderstanding the question?

EdocEvil
Posts: 14
Joined: Thu Nov 19, 2020 5:46 am

Re: How do you set the angle of a line graphic?

Post by EdocEvil » Sat Nov 21, 2020 9:08 am

I guess you could calculate the slope of your line from the coordinates for the two ends of your line.

Or if you knew the slope you wanted, and the coordinates of one end of your line, you could calculate the coordinates of the other end.

EE

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10115
Joined: Fri Feb 19, 2010 10:17 am

Re: How do you set the angle of a line graphic?

Post by richmond62 » Sat Nov 21, 2020 5:23 pm

I wonder why item 4 of the points of grc "PG2" is NOT returned, and what
I would expect to be item 4 is returned as item 3:
-
Points.jpg
-
Is this because there is no comma delimiter between the 2 lines?

And if so, how do I get the 4 items into their respective 'boxes'?

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10115
Joined: Fri Feb 19, 2010 10:17 am

Re: How do you set the angle of a line graphic?

Post by richmond62 » Sat Nov 21, 2020 5:31 pm

Oh, the erotic frisson from answering one's own questions:

Code: Select all

on scrollBarDrag
   put item 1 of line 1 of the points of grc "PG2" into fld "P1"
   put item 2 of line 1 of the points of grc "PG2" into fld "P2"
   put item 1 of line 2 of the points of grc "PG2" into fld "P3"
   put item 2 of line 2 of the points of grc "PG2" into fld "P4"
end scrollBarDrag
Last edited by richmond62 on Sat Nov 21, 2020 9:25 pm, edited 1 time in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10115
Joined: Fri Feb 19, 2010 10:17 am

Re: How do you set the angle of a line graphic?

Post by richmond62 » Sat Nov 21, 2020 6:14 pm

Well; there's no risk of this being described as perfect . . .
-
Screenshot 2020-11-21 at 19.11.59.png
-
Screenshot 2020-11-21 at 19.12.18.png
Attachments
Angler.livecode.zip
Here's the stack.
(2.7 KiB) Downloaded 230 times

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10339
Joined: Wed May 06, 2009 2:28 pm

Re: How do you set the angle of a line graphic?

Post by dunbarx » Sun Nov 22, 2020 5:17 am

This question came from the thread:
http://forums.livecode.com/viewtopic.php?f=7&t=34911

As soon as I wrote it, I realized that, as Stam pointed out, the question makes no sense on the face of it. And I am the one who used a single point as an origin, coupled sequentially with many other points as endpoints, all this to make a line act like a second hand.

Bernd had wonderful ideas in all this.

Anyway, I forgot to delete it.

I was really asking (myself?) for a method to draw a line given some parameters, something like:

Code: Select all

on mouseUp
   drawLine "100,100",30,100 --Origin, Angle, tLength in pixels
end mouseUp

on drawline tOrigin,tAngle,tLength
   create grc
   set the style of last grc to "line"
   add 90 to tAngle
   put tan(tAngle / (180 / pi)) into tRatio
   put tLength ^ 2 into hypot
   put (1 / tRatio) ^ 2 + 1 into factor
   put sqrt(hypot / factor) into y
   put y / tRatio into x
   set the points of last grc to tOrigin & "," & item 1 of tOrigin + y & "," & item 2 of tOrigin + x
end drawline
I am just fooling around here, and the above has issues, like getting it to work properly in all quadrants. But it is getting late...

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10339
Joined: Wed May 06, 2009 2:28 pm

Re: How do you set the angle of a line graphic?

Post by dunbarx » Mon Nov 23, 2020 4:24 am

So it was simple to implement into all quadrants. But there is a micro-kludge embedded. Anyway. this creates a line graphic starting somewhere at any angle at any length:

Code: Select all

on mouseUp
   drawLine "100,100",30,100 -- Origin, angle in degrees, length in pixels
end mouseUp

on drawLine tOrigin,tAngle,tLength
   create grc
   set the style of last grc to "line"
   put tAngle mod 360 into tAngle
   if tAngle = 0 then put 0.001 into tAngle
   put tan(tAngle / (180 / pi)) into tRatio
   put tLength ^ 2 into hypot
   put (1 / tRatio) ^ 2 + 1 into factor
   put sqrt(hypot / factor) into y
   put y / tRatio into x
   if tAngle <= 180 then
      set the points of last grc to tOrigin & "," & item 1 of tOrigin + y & "," & item 2 of tOrigin + x
   else
      set the points of last grc to tOrigin & "," & item 1 of tOrigin - y & "," & item 2 of tOrigin - x
   end if
end drawLine

You can do fun things like this:
[code]on mouseUp
   repeat with y = 1 to 36 
      put y * 10 into fld 1
      drawLine "100,100",y * 10,100
      wait 3
   end repeat
end mouseUp
Craig
Last edited by dunbarx on Tue Nov 24, 2020 3:38 pm, edited 2 times in total.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10054
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: How do you set the angle of a line graphic?

Post by FourthWorld » Mon Nov 23, 2020 4:53 am

dunbarx wrote:
Thu Nov 12, 2020 10:05 pm
One can set the angle of a polygon...
AFAIK that only applies to "regular" (fixed symmetrical) polygons.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10339
Joined: Wed May 06, 2009 2:28 pm

Re: How do you set the angle of a line graphic?

Post by dunbarx » Mon Nov 23, 2020 4:59 am

Richard.

Correct, which is the subject and solution of this thread. I made a gadget that draws a graphic where you need it at any angle at any length. Not technically the answer to my original question, but what I really meant and wanted.

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10115
Joined: Fri Feb 19, 2010 10:17 am

Re: How do you set the angle of a line graphic?

Post by richmond62 » Mon Nov 23, 2020 2:51 pm

AngledLine.jpg
-
NOT rocket science.
Attachments
Angled Line.livecode.zip
Here's the stack
(149.93 KiB) Downloaded 236 times

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10339
Joined: Wed May 06, 2009 2:28 pm

Re: How do you set the angle of a line graphic?

Post by dunbarx » Mon Nov 23, 2020 3:39 pm

Richmond.

I had done something like this in the second hand thread, making a large regular polygon of 1800 sides, and setting the points of a line from its center to a point on the polygon. Change that second point at intervals locked to a one minute total travel, and you get a smooth second hand that actually tells time.

I know that neither of our gadgets are rocket science.

But yours does not "draw" a line, based solely on origin, angle and, crucially, length. That requires a right triangle, the tangent function and a kludge.

Craig

EDIT. By "right triangle" I really meant to recall the unit circle, where the x and y coordinates of a line segment drawn from the origin define the tangent function.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10054
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: How do you set the angle of a line graphic?

Post by FourthWorld » Mon Nov 23, 2020 5:58 pm

FWIW I noticed the IDE provides a "Rotate Polygon" section in the "Objects" menu, and curious to see how it worked led me to this handler in revcommonlibrary.livecodescript:

Code: Select all

# Parameters
#   pGraphic : A reference to a graphic
#   pAngle : The angle to rotate the graphic by
# Description
#   Performs a rotation transformation whose origin is the loc of the graphic and
#   whose magnitude is pAngle degrees. Preserves the topLeft of the graphic, but may
#   cause distortion due to rounding errors.
command revRotatePoly pGraphic, pAngle
    local tPoints, tFinalPoints
    local tLoc
    local tSinAngle, tCosAngle
    local tCurrentH, tCurrentV, tTransformedH, tTransformedV
    local tOrigLeft, tOrigTop
    
    lock screen
    lock messages
    if the realPoints of pGraphic is empty then
        put the effective points of pGraphic into tPoints
    else
        put the realPoints of pGraphic into tPoints
    end if
    put the loc of pGraphic into tLoc
    put item 1 of tLoc into tOrigLeft
    put item 2 of tLoc into tOrigTop
    put sin(pi*pAngle/180) into tSinAngle
    put cos(pi*pAngle/180) into tCosAngle
    repeat for each line tPoint in tPoints
        if tPoint is not empty then
            put item 1 of tPoint into tCurrentH
            put item 2 of tPoint into tCurrentV
            put (tOrigLeft+(tCosAngle*tCurrentH)-(tSinAngle*tCurrentV), tOrigTop+(tCosAngle*tCurrentV)+(tSinAngle*tCurrentH) ) after tFinalPoints
        end if
        put cr after tFinalPoints
    end repeat
    delete char -1 of tFinalPoints -- is CR
    if the style of pGraphic is among the items of "oval,rectangle,roundrect,regular" then
        set the style of pGraphic to "polygon"
    end if
    set points of pGraphic to tFinalPoints
    set the realPoints of pGraphic to tFinalPoints
    set loc of pGraphic to tLoc
    unlock messages
    unlock screen
end revRotatePoly
It seems to support just about any angle provided in degrees, and works with both multi-sided polygons and line objects.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10339
Joined: Wed May 06, 2009 2:28 pm

Re: How do you set the angle of a line graphic?

Post by dunbarx » Mon Nov 23, 2020 6:55 pm

Richard.

That handler fails at the line:

Code: Select all

if the realPoints of pGraphic is empty then
That property is not yet defined, so there must be other stuff that happened before.

Anyway
if the style of pGraphic is among the items of "oval,rectangle,roundrect,regular" then
indicates that it does not deal with lines at all. Am I wrong?

There are ways, as the two threads mentioned indicate, to set the "length", "origin" and "angle" of an existing line graphic. None of these, however, draw such a graphic given only, er, a "length", "origin" and "angle". That is because a line graphic does not have any of those properties. They have to be given, and the graphic constructed from whole cloth.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10054
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: How do you set the angle of a line graphic?

Post by FourthWorld » Mon Nov 23, 2020 7:04 pm

dunbarx wrote:
Mon Nov 23, 2020 6:55 pm
Richard.

That handler fails at the line:

Code: Select all

if the realPoints of pGraphic is empty then
That property is not yet defined, so there must be other stuff that happened before.
So it would seem. I didn't try it in isolation before posting, only using the IDE's menu items.
Anyway
if the style of pGraphic is among the items of "oval,rectangle,roundrect,regular" then
indicates that it does not deal with lines at all. Am I wrong?
I can't say I had occasion to study the code in depth. I just opened the IDE, made a line object, made a two-point polygon, ran the IDE's menu items on each, and grabbed the code to post it here. If I get time later I may explore it in more detail; if you have time you may find something useful by tracing out the calls from the menu items to that handler to make sure those are being used as-is, that there may not be special handling for other types elsewhere in the chain.

All I can say for sure, based on my test before posting, is that the IDE provides a means of adjusting the angle of both line and polygon objects by arbitrary degrees, so we know the answer is in there somewhere.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply