Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2011-08-15 00:19:52

Mike George
Member
Registered: 2011-08-14
Posts: 14

calculating new coordinates for circular rotation to a new bearing

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

#2 2011-08-15 00:26:23

gAr
Member
Registered: 2011-01-09
Posts: 3,482

Re: calculating new coordinates for circular rotation to a new bearing

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

#3 2011-08-15 00:33:44

Mike George
Member
Registered: 2011-08-14
Posts: 14

Re: calculating new coordinates for circular rotation to a new bearing

Yes, that's it.:)

Offline

#4 2011-08-15 00:35:19

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: calculating new coordinates for circular rotation to a new bearing

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

#5 2011-08-15 00:38:08

gAr
Member
Registered: 2011-01-09
Posts: 3,482

Re: calculating new coordinates for circular rotation to a new bearing

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

#6 2011-08-15 00:49:59

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: calculating new coordinates for circular rotation to a new bearing

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

#7 2011-08-15 00:51:46

Mike George
Member
Registered: 2011-08-14
Posts: 14

Re: calculating new coordinates for circular rotation to a new bearing

bobbym wrote:

Hi;

Why not do it point by point.

Excuse my ignorance, but is that the same as


and

?

Offline

#8 2011-08-15 00:54:02

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: calculating new coordinates for circular rotation to a new bearing

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

#9 2011-08-15 00:59:00

Mike George
Member
Registered: 2011-08-14
Posts: 14

Re: calculating new coordinates for circular rotation to a new bearing

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

#10 2011-08-15 01:00:22

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: calculating new coordinates for circular rotation to a new bearing

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

#11 2011-08-15 01:03:16

Mike George
Member
Registered: 2011-08-14
Posts: 14

Re: calculating new coordinates for circular rotation to a new bearing

Anything between 0 and 359, so take your pick!

Offline

#12 2011-08-15 01:04:25

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: calculating new coordinates for circular rotation to a new bearing

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

#13 2011-08-15 01:13:38

Bob
Administrator
Registered: 2010-06-20
Posts: 10,059

Re: calculating new coordinates for circular rotation to a new bearing

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 smile

Offline

#14 2011-08-15 01:16:20

Mike George
Member
Registered: 2011-08-14
Posts: 14

Re: calculating new coordinates for circular rotation to a new bearing

The origin is at 50,50 but I'm sure I can cope with the translation Bob.

Offline

#15 2011-08-15 01:18:13

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: calculating new coordinates for circular rotation to a new bearing

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

#16 2011-08-15 01:27:04

Bob
Administrator
Registered: 2010-06-20
Posts: 10,059

Re: calculating new coordinates for circular rotation to a new bearing

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 smile

Offline

#17 2011-08-15 01:31:27

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: calculating new coordinates for circular rotation to a new bearing

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

#18 2011-08-15 02:04:30

Mike George
Member
Registered: 2011-08-14
Posts: 14

Re: calculating new coordinates for circular rotation to a new bearing

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

#19 2011-08-15 02:11:05

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: calculating new coordinates for circular rotation to a new bearing

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

#20 2011-08-15 06:49:49

Mike George
Member
Registered: 2011-08-14
Posts: 14

Re: calculating new coordinates for circular rotation to a new bearing

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?

Mike George wrote:

The polygon is drawn on an x,y coordinate system with the origin at top left.

Offline

#21 2011-08-15 06:55:05

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: calculating new coordinates for circular rotation to a new bearing

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

#22 2011-08-16 06:00:59

Mike George
Member
Registered: 2011-08-14
Posts: 14

Re: calculating new coordinates for circular rotation to a new bearing

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

#23 2011-08-16 06:03:17

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: calculating new coordinates for circular rotation to a new bearing

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

Board footer

Powered by FluxBB