在PHP中计算沿对角线y = x的距离 [英] Calculating distance along a diagonal y=x in PHP

查看:76
本文介绍了在PHP中计算沿对角线y = x的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





想象一下,我有一个从左下角到右上角有一条对角线的正方形。给定任意点(x,y),我希望能够确定该点在该线上的距离。这是因为我想创建从一种颜色到另一种颜色的颜色渐变,从正方形的左下角到右上角。



此图像显示我的工作这里;



给定任意点(90,50),我相信我应该计算形成的三角形的斜边沿着正方形的斜边开始。该点是直接对角线,因此它沿着正方形的斜边映射到位置z。



因此我应该能够执行以下操作来生成RGB值:



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);



从0到斜边长度映射到0-255。



任何人都可以帮我弄清楚为什么输出是这里 [ ^ ]?



有两个问题;渐变是完全错误的,右下方有一些黑色像素似乎选择不参与此代码。



我尝试过的:



----------------------------- -----------------------


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:

----------------------------------------------------

推荐答案

r = self :: map(hypot(
r = self::map(hypot(


y,


y)+(hypot(
y) + (hypot(


这篇关于在PHP中计算沿对角线y = x的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆