Use the following function to draw a line with a given angle and set length between two points.

```
/**
* Draw a line of a set length, from a given y,x
*
* @param resource $image
* The image resource.
* @param int $x1
* The X coordinate of the start of the line.
* @param int $y1
* The Y coordinate of the start of the line.
* @param int $length
* The length of the line, in pixels.
* @param float $angle
* The angle of the line, in degrees.
* @param int $color
* The colour resource, created with imagecolorallocate().
*/
function imageAngledLine($image, int $x1, int $y1, int $length, float $angle, $color) {
$x2 = $x1 + sin(deg2rad($angle)) * $length;
$y2 = $y1 + cos(deg2rad($angle + 180)) * $length;
imageline($image, $x1, $y1, $x2, $y2, $color);
}
```

This can be used in the following way.

```
$height = 500;
$width = 500;
$img = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($img, 255, 255, 255);
imagepolarline($img, 10, 10, 45, $white);
```

This assumes that you are using the GD2 library.

## Add new comment