渲染实心/填充多边形? [英] Rendering solid/filled polygons ?

查看:104
本文介绍了渲染实心/填充多边形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写自己的位图库来磨练我的计算机图形技能。我已经能够对其中的大部分进行编码,但是,我仍然必须编写能够渲染像圆盘,多边形等固体形状的函数。

我是难以决定如何解决这个问题。我应该使用布尔矩阵使用泛洪填充算法渲染完整的东西,然后将其复制回原始位图数据矩阵吗?或者我应该寻找一种肮脏的方式来填充我想渲染的形状?



这是我渲染多边形的代码:

I''m currently writing my own bitmap library to hone my computer graphics skills. I''ve been able to code the majority of it, however, I still have to code the functions that''ll render solid shapes like discs, polygons...etc.
I''m having difficulties deciding how to tackle this. Should I use a boolean matrix to render the full thing using flood fill algorithm, then copying it back to the original bitmap data matrix? Or should I look for a "mathy" way to fill the shapes I want to render?

Here is my code to render polygons:

void Bitmap::polygon(Point point, const unsigned int radius, const unsigned int sides, const unsigned char red, const unsigned char green, const unsigned char blue)
{
	float s = (float)sides;

	if((s >= 3) && (0 < radius))
	{
		const double spi = PI / 2;
		const double epi = -PI * 3 / 2;
		int x1;
		int y1;
		int x2;
		int y2;

		s = (float)(PI * 2 / s);

		x1 = point.x + (int)(cos(spi) * radius);
		y1 = point.y + (int)(sin(spi) * radius);

		{
		int j = 0;
		for(double i = spi - s; i >= epi; i -= s, j++)
		{
			x2 = point.x + (int)(cos(i) * radius);
			y2 = point.y + (int)(sin(i) * radius);
			bresenhamLine(this, x1, y1, x2, y2, red, green, blue);
			x1 = x2;
			y1 = y2;
		}
		}

		x2 = point.x + (int)(cos(spi) * radius);
		y2 = point.y + (int)(sin(spi) * radius);
		bresenhamLine(this, x1, y1, x2, y2, red, green, blue);
	}
}

推荐答案

你的直觉是正确的:高质量的图形库通常会填充mathy 方式,而不是通过辅助位图和洪水填充。对于圆形填充算法并不过分复杂。填充任意多边形有点困难。



查看圆形和椭圆形的Bresenham算法版本,以便快速找到边界点。这样可以节省大量的sin和cos调用,甚至可以使用纯整数运算。



对于多边形填充,我会(一如既往)从Google开始搜索。有很多有趣的算法。



祝你好运;你选择了一个有趣的主题。
Your instinct is right: High quality graphics libraries do the filling usually the "mathy" way and not via an auxiliary bitmap and flood fill. For a circle the filling algorithm is not overly complex. Filling an arbitrary polygon is somewhat more difficult.

Look into the versions of the Bresenham algorithm for circles and ellipses for a fast way to find the border points. That saves you a lot of sin and cos calls and you might even get away with pure integer arithmetic.

For polygon filling I would (as always) start with a Google search. There are quite some interesting algorithms around for that.

Good luck; you chose an interesting subject.


做任意多边形填充的最简单方法可能是将所有东西分成三角形,然后一次做一个三角形分段的老技巧。这对于圈子来说可能并不理想,但对于其他一切来说,它的速度相当快,数学也相当简单。



从内存中沿三角形的最长边迭代点,并计算每个点的正确长度,以获得与最长边成直角绘制的直线与在三角形之外。我确信有一些优化可以在没有完全重新计算的情况下为您提供每个行长。
Probably the easiest way to do arbitrary polygon filling is the old trick of dividing everything into triangles and then doing one triangular segment at a time. It''s probably not ideal for circles but for everything else it''s reasonably quick and the maths is reasonably simple.

From memory you iterate points along the longest side of the triangle and calculate the correct length at each point for a line drawn at right angles to the longest side to intersect the outside of the triangle. I''m sure there are optimisations to give you each line length without a full recalcuation.


这篇关于渲染实心/填充多边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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