Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Imagine I have a square with a diagonal line from the bottom left to the top right corner. Given any point (x,y), I want to be able to determine how far along that line the point is. This is because I want to create a colour gradient from one colour to another, from bottom left to top right of a square.

This image displays my working here;

Given the arbitrary point (90,50), I believe I should calculate where the hypotenuse of the formed triangle begins along the square's hypotenuse. The point is directly diagonal, so it maps to position z along the square's hypotenuse.

Therefore I should be able to do the following to generate an RGB value:

$r = self::map(hypot($y, $y) + (hypot($x, $y) / 2), 0, hypot($imgW, $imgH), 0, 255);
$c = ImageColorAllocate($base_image, 0, $r, $r);

by mapping from 0 to the hypotenuse length, to 0-255.

Could anyone please help me to figure out why the output is this here[^]?

There are two issues; the gradient is completely wrong, and there are some black pixels in the bottom right which have seemingly chosen not to participate in this code.

What I have tried:

----------------------------------------------------
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900