PHP - 多边形 GD 库中的错误?

发布于 2024-08-07 08:42:38 字数 1332 浏览 4 评论 0原文

我在 gd 库的 imagefilledpolygon() 方面遇到了一些麻烦。

由于某种原因,我的一些线条最终错位了 1px,因此我决定使用 imagepixelset 对其进行调试,将形状点的颜色设置为红色。

替代文本 http://www. Degreeshowcase.com/other/1.gif 如果你看图片,你会发现有些点在形状内部……有些在外部……这非常不合逻辑。

(图片已放大,以便更明显)

有人有解决方案吗?

更新:

我对上面形状的观点是: 0,0 40,0 40,20 20,20 20,40 0,40

我要求生成的形状的高度和宽度应该是 20 的倍数......但是由于某种原因,某些形状最终的高度或宽度为 21 像素。

我已经制作了一个脚本来找出获得我想要的形状的要点,但我无法弄清楚为什么,所以我无法制定一个脚本来纠正我的所有形状。

<?php

// set up array of points for polygon
$values = array(0,0, 39,0, 39,20, 19,20, 19,39, 0,39);

//My original values were 0,0 40,0 40,20 20,20 20,40 0,40
//I do not understand why some values require minus 1 and others can remain as they were (a multiple of 20)

// create image
$image = imagecreatetruecolor(40, 40);

// allocate colors
$bg   = imagecolorallocate($image, 200, 200, 200);
$blue = imagecolorallocate($image, 0, 0, 255);

// fill the background
imagefilledrectangle($image, 0, 0, 39, 39, $bg);

// draw a polygon
imagefilledpolygon($image, $values, 6, $blue);

// flush image
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);


?>

I have run into some trouble with the gd library's imagefilledpolygon().

For some reason some of my lines were ending up 1px out of place so I decided to debug it using imagepixelset to set the colour of my shapes points to red.

alt text http://www.degreeshowcase.com/other/1.gif
if you look at the picture you can see some of the points are inside the shape ... some are outside....its very illogical.

(the picture has been scaled up to make it more visible)

Does anyone have a solution?

Update:

My points for the shape above were: 0,0 40,0 40,20 20,20 20,40 0,40

I require that the height and width of the shape produced should be a multiple of 20.... but for some reason some of the shape ends up 21 px high or wide.

I have made a script to work out what the points would be to get the shape I wanted but I can not work out why and so I can't work out a script to correct all my shapes.

<?php

// set up array of points for polygon
$values = array(0,0, 39,0, 39,20, 19,20, 19,39, 0,39);

//My original values were 0,0 40,0 40,20 20,20 20,40 0,40
//I do not understand why some values require minus 1 and others can remain as they were (a multiple of 20)

// create image
$image = imagecreatetruecolor(40, 40);

// allocate colors
$bg   = imagecolorallocate($image, 200, 200, 200);
$blue = imagecolorallocate($image, 0, 0, 255);

// fill the background
imagefilledrectangle($image, 0, 0, 39, 39, $bg);

// draw a polygon
imagefilledpolygon($image, $values, 6, $blue);

// flush image
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);


?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薔薇婲 2024-08-14 08:42:38

我的猜测是你混淆了宽度和位置。

例如,从 0px 到 9px 的线长 10px...如果您使用长度作为第二个参数而不是位置,则最终长度将是 11px。

如果我能看到一些代码我就可以确认这一点。

My guess is that you're mixing up width with position.

For example a line from 0px to 9px is 10px long... if you used length as the second parameter instead of position, it would end up 11px long.

If I could see some code I could confirm this.

淡笑忘祈一世凡恋 2024-08-14 08:42:38

正常的多边形渲染可确保每个像素只能位于一个多边形中(如果两个多边形共享一条边)。如果您想象绘制两个彼此相邻的正方形,以便它们共享公共边缘,那么您不希望沿着该边缘渲染像素两次。

这里有关于确定多边形边缘上的哪些像素应被视为多边形内部的解释: http://www.gameprogrammer.com/5-poly.html

一个常见的解决方案是说“多边形左边缘和上边缘的像素属于该多边形,右边缘和下边缘的像素不属于该多边形” 't"。我不是 100% 确定 GD 使用什么解决方案,因为我找不到任何相关文档,但我希望它与此类似。

Normal polygon rendering ensures that each pixel can only be in one polygon, if the 2 polygons share an edge. If you imagine drawing 2 squares, next to each other, so they share a common edge, you don't want to render the pixels along that edge twice.

There is an explanation of determining which pixels on the edge of a polygon should be considered inside the polygon here: http://www.gameprogrammer.com/5-poly.html

A common solution is to say that "pixels on the left and top edges of a polygon belong to the polygon and pixels on the right and bottom edges don't". I am not 100% sure what solution GD uses, as I could not find any documentation on it, but I expect it is something similar to this.

波浪屿的海角声 2024-08-14 08:42:38

我与目前开发 GD 库的人交谈过,他解释说它遵循“绕数算法” - 可以找到

据我所知,使用此函数准确(到像素)生成凹多边形的唯一方法是编写另一个函数,该函数也将缠绕规则应用于您的坐标并相应地调整它们,然后将其放入函数中。

I spoke to the guy who currently develops the GD library he explained that it follows the 'Winding number algorithm' - can be found here. Having looked at my example image, it does match how the 'winding number algorithm' works, however the function should take this into account and produce the shape that was input.

As far as I can see the only way to accurately (to the pixel) generate a concave polygon with this function is to write another function that also applies the winding rule to your coordinates and adjusts them accordingly and then put it into the function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文