You are not logged in.
I'm trying to write a function to turn a polygon to a specified bearing.
The polygon is drawn on an x,y coordinate system with the origin at top left.
I know the coordinates for the centre of the polygon, and for the starting position of each point on the polygon, and the value in degrees of the angle the polygon has to be rotated through (clockwise).
Could somebody give me the forumula to apply to each point to do this please (or explain how to work it out)?
Offline
Hi Mike George,
Are you saying you need to find the positions of the vertices after rotation?
"Believe nothing, no matter where you read it, or who said it, no matter if I have said it, unless it agrees with your own reason and your own common sense" - Buddha?
"Data! Data! Data!" he cried impatiently. "I can't make bricks without clay."
Offline
Yes, that's it.:)
Offline
Hi;
Why not do it point by point.
In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.
Offline
Yes, bobbym said it!
"Believe nothing, no matter where you read it, or who said it, no matter if I have said it, unless it agrees with your own reason and your own common sense" - Buddha?
"Data! Data! Data!" he cried impatiently. "I can't make bricks without clay."
Offline
Hi all;
That is the formula if you want to rotate it around the origin.
Hi Mike;
Do you have some points to use for the demonstration?
In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.
Offline
Hi;
Why not do it point by point.
Excuse my ignorance, but is that the same as
Offline
Hi Mike;
Not quite. Want to try again, or need help?
In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.
Offline
I could do with more help please.
The array of vertices for an arrow to be rotated is:
$arrowVertices = array(
50, 10,
70, 30,
55, 30,
55, 90,
45, 90,
45, 30,
30, 30,
50, 10
);
Offline
Check post #8.
What angle?
In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.
Offline
Anything between 0 and 359, so take your pick!
Offline
Okay, I will try 135 degrees and it will be counter clockwise and around ( 0 , 0 ).
In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.
Offline
hi Mike George,
The matrix method of rotation is excellent if (and only if) the origin (0,0) is the centre of the rotation. Is this ok for you?
If not you'll have to translate the shape until this condition is met, then rotate, then translate again.
In your example (post #9) you do not say where the centre is.
Bob
Last edited by Bob (2011-08-15 01:14:03)
Children are not defined by school ...........The Fonz
You cannot teach a man anything; you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you! …………….Bob
Offline
The origin is at 50,50 but I'm sure I can cope with the translation Bob.
Offline
You will have to translate ( 50, 50 ) to ( 0, 0 )
In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.
Offline
Ok. So as follows:
(i) Translate all points by (-50, -50). Origin is now the centre.
(ii) Use bobbym's matrix to rotate any angle, to obtain new co-ordinates.
(iii) Translate all points back by (+50, +50) so that the centre is back where it started. => it was invariant under the transformations.
Bob
Children are not defined by school ...........The Fonz
You cannot teach a man anything; you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you! …………….Bob
Offline
Hi
Translation to (0,0)
Rotate 135 degrees.
Back to (50,50)
x is the first column. It would have been better if the points would have been more recognizable as a polygon.
In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.
Offline
Thanks bobbym, but I'm afraid you've lost me - can we go back a step?
Taking the first point, where x = 50 and y = 10, I understand I have to compensate for the offset of the centre and make x = 0 and y = -40, then reverse this after the rotation.
I'm really just looking for a formula to apply to x and a formula to apply to y, to give the new coordinates to rotate the point t degrees (preferably clockwise, but that's not important).
The polygon described by the list I gave is an arrow on a bearing of 0 degrees, which is the shape I will be using.
Offline
Hi;
The original point is ( 50, 10 )->(0, -40)
Then take ( 0, - 40 ) with x = 0 and y = -40, t = 135 degrees counter clockwise. The new points are x' and y'.
Now just add 50 to both x and y.
Also, I think the first point should be something like ( 50, 100 ), makes a better arrow.
In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.
Offline
The box it has to fit in is only 100x100, so using 50,100 would put the point of the arrow at the bottom and I want it to point North if no angle is specified.
I've tried implementing the formula without much success so far. Could it be it doesn't take account of this?
The polygon is drawn on an x,y coordinate system with the origin at top left.
Offline
Hi Mike;
If the origin is at the top left then the points are in quadrant 4. The signs of the points you provided are in quadrant one. Once you have the correct signs the rotation will work just the same.
In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.
Offline
Aha!
Many thanks for your help and your patience. My script now does what it is supposed to andyou can see a demo in action at shed5.co.uk/demo.rotatearrow.gif.php?b=35
For anybody else looking for help to rotate a polygon using the PHP GD library of image functions, this is the script I ended up with. It's a bit long-winded to help make it clearer:
<?php
$im = @imagecreatetruecolor(100, 100); // create the image resource
$white = imagecolorallocate($im, 255, 255, 255); // fill the background of
imagefill($im, 0, 0, $white); // the image with white
$fill = imagecolorallocate($im, 0xFF, 0x33, 0x33); // allocate the colour to fill the arrow
if (isset($_GET['b'])) {
$bearing = -1*deg2rad($_GET['b']); // the angle to turn the polygon, clockwise
} else {
$bearing = 0;
}
$offsetX = 50; // the coordinates of the centre
$offsetY = -50; // of the polygon
$arrowVerticesBase = array( // the coordinates for the polygon
array(50, -10), // in its normal state
array(70, -30), // (this one is an arrow pointing north)
array(55, -30), // the y coordinates are negative
array(55, -90), // because the polygon is in the
array(45, -90), // 'fourth quadrant' with the origin
array(45, -30), // of the grid at top left
array(30, -30),
array(50, -10)
);
$arrowVertices = array(); // the array to hold coordinates for the rotated polygon
foreach($arrowVerticesBase as $vertex) { // for each x,y pair of coordinates
$x = $vertex[0] - $offsetX; // take away the offset to put the centre of the polygon
$y = $vertex[1] - $offsetY; // at the origin of the grid
$x1 = $x * cos($bearing) - $y * sin($bearing); // finally, this is the formula that
$y1 = $y * cos($bearing) + $x * sin($bearing); // does the rotation
$x1 = $x1 + $offsetX; // add back the offsets to put the centre
$y1 = $y1 + $offsetY; // back in its proper position
$arrowVertices[] = $x1; // put the new coordinates into
$arrowVertices[] = -1*$y1; // the array for the function (make the y values positive for the imagefilledpolygon() function)
}
$arrow = imagefilledpolygon( // create the rotated polygon
$im, // the image resource
$arrowVertices, // the array of coordinates to use
sizeof($arrowVertices)/2, // the number of coordinate pairs in the array
$fill // the fill colour for the polygon
);
header ('Content-Type: image/gif'); // display the image
imagepng($im);
imagedestroy($im);
?>
Offline
Hi;
Glad you got it working! Good luck programming.
In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.
Offline