Jump to content

Say I have two points on a picture


Prismatic

Recommended Posts

And I connect them with a straight line, 1 pixel wide.

 

How would I go about tracing that line, starting at point A, and going to B?

 

I'm working on an application and will need to check certain values at each point along a line.. I'm not actually drawing a line, but if a line were there, i would need to be able to follow it.

 

See my graphic for an example,

 

ideas?

 

Edit - Basically im asking how to find the coordinates of each pixel along a line between 2 coordinates

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/136770-say-i-have-two-points-on-a-picture/
Share on other sites

You'd be better off writing an algorithm to work that out

 

imageine two point (x,y);

 

1: 10,10

2: 100,50

 

if you need to find 2 points along that line;

 

(ill start writting in php)

 

/*
*Set known variable
*Start position
*End position
*Number of extra points
*/
$start['x'] = 10;
$start['y'] = 10;
$end['x'] = 100;
$end['y'] = 50;
$midPoints = 2;

/*
*Figure out x and y for mid points
*/
$xDistance = $end['x'] - $start['x']; //90
$yDistance = $end['y'] - $start['y']; //50

$xMidPointsDistance = $xDistance / ($midPoints + 1); //90 / (2+1) =  30
for($i=0;$i<$midPoints;$i++){
$xMidPoint[$i] = ($xMidPointsDistance * ($i + 1)) + $start['x'];
}

$yMidPointsDistance = $yDistance / ($midPoints + 1); //50 / (2+1) =  16.66
for($i=0;$i<$midPoints;$i++){
$yMidPoint[$i] = ($yMidPointsDistance * ($i + 1)) + $start['y'];
}

for($z=0;$z<$midPoints;$z++){
echo $xMidPoints[$z].', '.$yMidPoints[$z].'<br />';
}

 

Not tested, and was just me guessing what you needed ;)

if you have 2 points (x0, y0) and (x1, y1) then for any value of x, the value of y is given by

 

$y = $y0 + ($x - $x0) * $m;

 

where

$m = ($y1 - $y0)/($x1 - $x0);

 

Example

<?php
$x0 = 10;
$y0 = 10;
$x1 = 100;
$y1 = 50;

$m = ($y1 - $y0)/($x1 - $x0);

$im = imagecreate(100,100);
$bg = imagecolorallocate($im,0,0,0);
$plot = imagecolorallocate($im, 0xFF,0xFF,0xFF);

for ($x=$x0; $x <= $x1; $x++) {
    $y = $y0 + ($x - $x0) * $m;
    imagesetpixel($im,$x,$y,$plot);
}

header("content-type: image/png");
imagepng($im);
imagedestroy($im);
?>

I've revised (and simplified) the equation into the more usual Y = MX + C where M is the slope of the line and C a constant for the Y value when X is 0.

 

<?php
function mark ($im, $x, $y, $col)
{
    // plot points on the line with an X
    imageline($im, $x-2, $y-2, $x+2, $y+2, $col);
    imageline($im, $x+2, $y-2, $x-2, $y+2, $col); 
}

$x0 = 10;
$y0 = 10;
$x1 = 100;
$y1 = 50;

$m = ($y1 - $y0)/($x1 - $x0);                        // slope
$c = $y0 - $m*$x0;                                   // constant

$im = imagecreate(150,100);
$bg = imagecolorallocate($im,0,0,0);
$line = imagecolorallocate($im, 0xCC,0xCC,0xCC);
$plot = imagecolorallocate($im, 0xFF,0xFF,0x00);
imageline($im, $x0, $y0, $x1, $y1, $line);

for ($x=$x0; $x <= $x1; $x+=20) {
    $y = $m*$x + $c;                                 // rearranged to Y = MX + C
    mark($im,$x,$y,$plot);
}

imagestring($im, 2, 1,80,sprintf("y = %0.2f * x + %d", $m, $c), $plot);   // equation
header("content-type: image/png");
imagepng($im);
imagedestroy($im);

?>

 

So in the example, y = 0.44 * x + 5

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.