How to rotate a square

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: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
MadDogDean
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 36
Joined: Thu Jan 03, 2013 6:33 pm
Location: Floating in the binary mist

How to rotate a square

Post by MadDogDean » Mon Jan 26, 2015 7:27 am

Good day all,

So, it's pretty simple what I am trying to do, but it's not sinking in...
How can I create a 100 px wide square, have the ability to rotate it and actually have it keep to right size?
Using LC 7.0.1 I was playing with the Polygon, Regular polygon, Rectangle, etc, etc and got the following:
- If I create a "Rectangle" 100 px wide, I can not rotate it
- If I create a "Regular Polygon" 100 px wide, I need to rotate it 45° to sit square, but the dimensions are smaller (the 100 px seems to reference the "width" when the square is standing on one corner


There's been lots of discussions about rotating "things" but this still seems to be a very difficult process :shock:

Attached is a screen shot of what I end up with.
--- ** EDIT ** ignore the image of the "Polygon"

Bottom line, is there a way to create a "Regular Polygon" object with a specified length of a side and be able to rotate it arbitrarily?

Thanks to all.
Dean
rect.jpg

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

Re: How to rotate a square

Post by Simon » Mon Jan 26, 2015 7:49 am

hmmm... this is a weird one.
Try just drawing a rectangle then this;

Code: Select all

on mouseUp
   set the style of grc 1 to regular
   set the angle of grc 1 to the angle of grc 1 + 10
end mouseUp
Or maybe you have already.

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

MadDogDean
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 36
Joined: Thu Jan 03, 2013 6:33 pm
Location: Floating in the binary mist

Re: How to rotate a square

Post by MadDogDean » Mon Jan 26, 2015 8:58 am

Thanks Simon,
Hadn't tried it like that yet, but did with same results. Once the Rectangle style is changed to a Regular polygon, it rotates up on one point and the given dimension is applied to the height/width (based on corner to corner). Rotating it 45° and it's too small again...

To see what I am seeing, I created a new mainstack and ran this code in the Message box:

Code: Select all

   create invisible graphic "rectangle"
   set width of graphic "rectangle" to 100
   set height of graphic "rectangle" to 100
   show graphic "rectangle"
   
   wait 2 seconds
   
   lock screen
   set style of graphic "rectangle" to regular
   set angle of graphic "rectangle" to 45
   unlock screen
Cheers,
Dean

MadDogDean
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 36
Joined: Thu Jan 03, 2013 6:33 pm
Location: Floating in the binary mist

Re: How to rotate a square

Post by MadDogDean » Mon Jan 26, 2015 9:06 am

OK, so here it goes again... For some reason, I couldn't get revRotatePoly to work earlier - it kept crashing LC 7.0.1, but now it seems to work - sort of.

If I

Code: Select all

revRotatePoly the name of graphic "Rectangle", 45
- it converts the rectangle to a polygon, it rotates it 45 deg (as specified), but the left side goes missing...

Cheers,
Dean
poly.jpg
--- ** EDIT ** this same action happens with either a rectangle or a rounded rectangle, but NOT with a multi-sided irregular polygon

--- ** EDIT 2 ** If a rectangle is rotated using revRotatePoly, it appears as though the 5th set of points gets lost/ignored (the "closing" point). If these are manually entered after rotating the poly, the side closes up. Looks more like a bug at this point. I'll have to reload LC 6.7 and give it a try and submit a bug report if it continues.

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

Re: How to rotate a square

Post by SparkOut » Mon Jan 26, 2015 10:15 am

A graphic's dimensions always relate to a square, with the minimum measurements being that required to contain the graphic given its orientation. So while a rectangle can have a width of 100 and you can see the length of the side being 100, when you come to a regular polygon the width does not relate to the length of the side, it is the length of the area required to contain the graphic. The first point of a regular polygon at angle 0 is at the top centre, so a 4 sided regular polygon is equivalent to a square of angle 45 but the length of the sides will be reduced to fit the dimensions of the graphic bounding rect. In this case the width is the diagonal across the middle of the square, which is a simple calculation in terms of pythagoras to relate the sum of the squares of the two adjacent sides to the square of the hypotenuse. To expand the length of the sides of the 4 sided regular polygon to be 100, the width of the graphic has to be expanded to cater for the longer hypotenuse.

Code: Select all

on mouseUp
   --with a rectangle graphic and a regular polygon of 4 sides on the card
   set the width of grc "grcSquare" to (random (100) +50)
   set the height of grc "grcSquare" to the width of grc "grcSquare"
   set the loc of grc "grcSquare" to the loc of this card
   put fnPyDim(the width of grc "grcSquare") into tPolyDim
   set the width of grc "grcPoly" to tPolyDim
   set the height of grc "grcPoly" to tPolyDim
   set the angle of grc "grcPoly" to 45
   set the loc of grc "grcPoly" to the loc of grc "grcSquare"
end mouseUp

function fnPyDim pSide
   return sqrt(pSide * pSide * 2)
end fnPyDim
For other regular polygons the calculations are also possible, but I couldn't bring myself to make it a generic function

MadDogDean
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 36
Joined: Thu Jan 03, 2013 6:33 pm
Location: Floating in the binary mist

Re: How to rotate a square

Post by MadDogDean » Mon Jan 26, 2015 11:15 am

Thanks SparkOut,

Thanks for your input, always valued. I guess I was a little lost as to why this occurs the way it does. Although there appears to be a small bug (I've submitted a bug report update) using revRotatePoly, the dimensions of the "Polygon" compared to a "Regular Polygon" are updated and maintained as they should be.

eg. The dimensions of a 100px x 100px Polygon, rotated 45 deg changes to 143px x 143px; rotate another 45 deg and the dimensions change again to 100px x 100px.

In the case of a Regular Polygon, the dimensions are always 143px x 143px, regardles of the angle of rotation of the graphic.

With the submitted bug report update, I hope the problem will be resolved using revRotatePoly instead of angle (at least for now...)

Cheers,
Dean

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: How to rotate a square

Post by bn » Mon Jan 26, 2015 11:50 am

Hi Dean,

try to set the opaque of the graphic to true before rotating. For a polygon it will close the shape by adding a final point that is the same as the first point of the graphic.

Once you have set it to opaque you can revert to opaque to false. The missing point will be part of the graphic.

Regarding dimensions of rectangle vs polygon of same size see this stack I uploaded here

http://forums.livecode.com/viewtopic.ph ... 31#p106602

I see you have commented on bug 12692 and Bug 13712

Could you post a stack that shows what exactly you are trying to do? Because the problems of this are in the "fine-print".
Do you use the effective points to convert from a Regular Polygon to Polygon?

As I said, try setting the opaque of the graphic to true do your rotating and if you don't want the graphic to remain opaque change it back to non-opaque.

Kind regards
Bernd

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: How to rotate a square

Post by bn » Mon Jan 26, 2015 1:04 pm

Hi Dean,

if you use one of the top graphic objects from the palette then this works for me in LC 6.7.1 and LC 7.0.1 and the release candidates after that.

Code: Select all

on mouseUp
   set the points of grc 1 to the effective points of grc 1
   set the opaque of grc 1 to true
   set the style of grc 1 to polygon
   revRotatePoly the name of graphic 1, 45   
end mouseUp
Kind regards
Bernd

MadDogDean
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 36
Joined: Thu Jan 03, 2013 6:33 pm
Location: Floating in the binary mist

Re: How to rotate a square

Post by MadDogDean » Tue Jan 27, 2015 2:19 am

@Bernd,

Thanks for the explanation, yes I did read the forum posting late last night and it makes *more* sense now...

As for a sample stack of what I am doing, this isn't something that actually I need, but I was walking through playing with LC when I tripped over it. I was unable to let go until I had it figured out (o.O)

Now another issue pops up, and maybe it is my inexperience with LC, but this is for others trying to figure it out If you resize a polygon and then try to rotate it, it will revert to the original size....

For example:
Create a random sized rectangle freehand using the tool palette tool, assume this rectangle is 106px x 128px
Open the Inspector and change the size to 100px x 100px (it is still of style "rectangle")
If you rotate this via code (like your sample code), the size of the rectangle reverts back to the original dimension of 106px x 128px
- you need to FIRST set the "Lock size and position" using the Inspector or set lockLoc of grc 1 to true


After you rotate the graphic you can turn the lockLoc off again and the polygon maintains the correct size.

Cheers,
Dean

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: How to rotate a square

Post by mwieder » Wed Jan 28, 2015 4:31 am

Dean-

I added a comment to bug report 13712 - the missing endpoint has been corrected in the engine but the rotation script is still awaiting a fix in the IDE library stack.

But please do file a new bug report on the resize thing... you're quite right that rotation after resizing the original graphic reverts the graphic to its original size. I *think* this is not the correct behavior, but it may also be the case that reverting to original size without locking the graphic is desired and/or necessary in some cases, and I'd like to get some feedback from the team before fixing it.

And thanks for uncovering this problem - it's another edge case we hadn't considered.

MadDogDean
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 36
Joined: Thu Jan 03, 2013 6:33 pm
Location: Floating in the binary mist

Re: How to rotate a square

Post by MadDogDean » Thu Jan 29, 2015 2:08 am

Hey Mark,

The bug report will have to wait for now. I repeated my exact steps to recreate the problem - and it worked as it is supposed to. Problem fixed itself!!!

Actually, I've run into odd occurrences now and then and should have known to reboot. It seems with LC, if running the IDE all day and enduring a couple "Hibernates", LC has some odd quirks, slow downs., etc. This resizing thing appeared to be an odd quirk that has fixed itself, but I'll keep it in my radar.

Cheers,
Dean

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: How to rotate a square

Post by mwieder » Thu Jan 29, 2015 5:34 am

Ah, indeed. I had tested it with earlier versions, and it did as you described, but that seems fixed now in current 7.x incarnations.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”