如何光栅化旋转的矩形(在 2d 中由 setpixel) [英] how to rasterize rotated rectangle (in 2d by setpixel)

查看:27
本文介绍了如何光栅化旋转的矩形(在 2d 中由 setpixel)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旋转矩形的四个二维顶点 A B C D,我需要在 pixelbufer 中(有效地)光栅化/绘制它使用 setpixel(x,y,color)

I have got a four 2d vertices A B C D of rotated rectangle, I need to rasterize/draw it (efficiently) in pixelbufer with setpixel(x,y,color)

怎么做?

我正在尝试使用类似的代码

i was trying with some code like

    // convertilg a b c d do up down left right, 
    // calculating some dx_left dx_right on y--
    // etc (frustrating on special cases when there are 2 up_y vertices in same line etc)


    for(;;)
    {

     drawhorizontalline(y, xstart, xend, color);

     if(y==downy) break;

     y--;
     xstart+=dxstart;
     xend+=dxend;

     if(y==lefty)  dxstart = dxright;
     if(y==righty) dxend = dxleft;

     }

但它是最令人沮丧的(非常容易出现错误并且最令人沮丧)我真的厌倦了昨天一整天的调试,我需要找到也许是一些工作代码而不是尝试调试这个

but it is most frustrating (terribly bug prone and most frustrating) i am really tired of debuging this all day yesterday and i need to find maybe some working code rather than to try to debug this

推荐答案

要将矩形填充为闭合凸多边形处理(与三角形填充几乎相同)

To fill your rectangle handle it as closed convex polygon (almost the same as triangle filling)

  1. 根据绕线规则排列您的积分

所以有行AB BC CD DA 或反向

创建左右缓冲区

address 是 y-coordinate,它是 x-positions 的数组,如果需要,还有 color、texture 坐标、....初学者:

address is y-coordinate, its an array of x-positions and if needed also array of color,texture coordinates,.... for starters:

`int buf_x0[ys],buf_x1[ys];`

其中 ys 是屏幕 y-resolution

where ys is screen y-resolution

实现任何绘制线算法

但不是绘制到屏幕,而是将像素的 x 坐标存储到缓冲区.

but instead of draw to screen just store x coordinate of pixel to buffer.

  • 代替:setpixel(x,y,color); 做:buf_x?[y]=x;.

哪个缓冲区是目的地取决于线路Y方向

Which buffer is the destination depends on the line Y direction

  • 如果dy<0则填充buff_x0
  • 如果dy>0则填buff_x1
  • 如果 dy==0 那么 buf_x0[y]=min(x)buf_x1[y]=max(x)
  • if dy<0 then fill buff_x0
  • if dy>0 then fill buff_x1
  • if dy==0 then buf_x0[y]=min(x) and buf_x1[y]=max(x)

请注意,您必须在光栅化之前按 x 坐标对线端点进行排序,以避免因反向端点线产生的不同像素而导致网格中出现接缝/孔洞.

Beware you have to sort the line endpoints by x coordinate before rasterizing to avoid seams/holes in meshes caused by different pixels produced for reversed endpoints lines.

将此线算法应用于多边形的所有边界线(AB、BC、CD、DA)

在此之后,缓冲区包含开始和结束 x-水平线的位置

after this the buffers contains start and end x-positions of your horizontal lines

填充屏幕上的矩形

for (y=min(Ay,By,Cy,Dy);y<=max(Ay,By,Cy,Dy);y++)
 draw_horizontal_line(y,buf_x0[y],buf_x1[y],color);

图像清晰(取自我关于低级计算机图形的讲座)

Image for clarity (taken from my lectures on low level computer graphics)

图片说明:

  • 垂直矩形表示边界缓冲区 buf_x0[],buf_x1[]
  • 顺时针缠绕规则确保目标缓冲区.如果它比 buf_x0[y] <= buf_x1[y] 正确编码,那么水平线的绘制会折叠为单个 for 循环
  • vertical rectangles represents the border buffers buf_x0[],buf_x1[]
  • clockwise winding rule ensures the destination buffer. If its coded properly than buf_x0[y] <= buf_x1[y] so draw of horizontal line colapses to single for loop

还有这里我的简单C++示例

这篇关于如何光栅化旋转的矩形(在 2d 中由 setpixel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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