Page 1 of 1

Position calculation problem.

Posted: Wed Dec 02, 2009 1:45 pm
by alm2009
Hi, I have some position calculation problem.
In the code below I draw a big circle (R=400). And I draw a Small (r=50) circle whish should positioned on the perimeter of the BIG circle.
The program change the position of the Small circle according the $degree variable.
Every thing works except when I calculate the position of the center for the small circle I use
X = R * sin(deg2rad($degree))
Y = R * cos(deg2rad($degree))

Here is my problem: when I use the original value of R then my result for the center of the small circle is way off (see red circle).
I can fix that using R/2 in my calculation and then it’s in the right position (see blue circle).
But I need to know where is my mistake for the further calculations.

Thanks in advance.

Code: Select all

 
<?php
$size_x = 600;
$size_y = 600;
 
// Create a blank image.
$image = imagecreatetruecolor($size_x, $size_y);
 
// Select the background color.
$Black_color = imagecolorallocate($image, 0, 0, 0);
$Red_color = imagecolorallocate($image, 255, 0, 0);
$Blue_color = imagecolorallocate($image, 0, 0,255);
$Fill_color1 = imagecolorallocate($image, 230, 230, 230);
 
// Fill the background with the color selected above.
imagefill($image, 0, 0, $Fill_color1);
 
// Draw the lines.
// vertical line
$x1 = $size_x / 2; $y1 = 50; $x2 = $size_x / 2; $y2 = $size_y-50;
imageline($image, $x1, $y1, $x2, $y2, $Black_color);
 
// horizontal line
$x1 = 50;  $y1 = $size_y / 2; $x2 = $size_x-50; $y2 = $size_y / 2;
imageline($image, $x1, $y1, $x2, $y2, $Black_color);
 
// 45 degree line
$x1 = $size_x - (($size_x-25) * sin(45)); $y1 = $size_y - $x1; $x2 = $size_x -$x1; $y2 = $x1;
imageline($image, $x1, $y1, $x2, $y2, $Black_color);
 
// 180 - 45 degree line
$x1 = $size_x - (($size_x-25) * sin(45)); $y1 = $x1; $x2 = $size_x -$x1; $y2 = $size_y - $y1;
imageline($image, $x1, $y1, $x2, $y2, $Black_color);
 
// Draw the BIG circle.
$R = 400;
imageellipse($image, $size_x / 2, $size_y / 2,$R, $R, $Black_color);
 
// Path ttf font file
$font_file = 'arial.ttf';
 
// Draw the text  using font size 10
imagefttext($image, 10, 0, ($size_x/2)-12, 65, $Black_color, $font_file, '0');
imagefttext($image, 10, 0, $size_x-75, ($size_x/2)-5, $Black_color, $font_file, '90');
imagefttext($image, 10, 0, 55, ($size_x/2)-5, $Black_color, $font_file, '270');
imagefttext($image, 10, 0, ($size_x/2)+5, $size_y-60, $Black_color, $font_file, '180');
 
$r = 50;
$degree = 45;
 
// Draw the red circle  
    $x_i = ($R)*sin(deg2rad($degree));
    $y_i = ($R)*cos(deg2rad($degree));
    imageellipse($image, 300+$x_i, 300-$y_i, $r, $r, $Red_color);
 
// Draw the blue circle 
$R = 200;   
    $x_j = ($R)*sin(deg2rad($degree));
    $y_j = ($R)*cos(deg2rad($degree));
    imageellipse($image,300+$x_j, 300-$y_j, $r, $r, $Blue_color);
 
// Output the image.
header("Content-type: image/png");
imagepng($image);
?>
 
 

Re: Position calculation problem.

Posted: Wed Dec 02, 2009 2:08 pm
by alm2009
Problem solved!
This is the second time here, I am struggling with the problem for couple of days then I post the topic. And immediately after that I realize what my problem was.

imageellipse ( resource $image , int $cx , int $cy , int $width , int $height , int $color )

The function above use $with and $height as diameter when you drawing a circle (and not as radius as I wrongly assumed).

Cheers