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

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

问题描述

我有旋转矩形的四个顶点的2D ABCD, 我需要光栅化/画它(有效)的pixelbufer 用与setPixel(X,Y,色)

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)

该怎么办呢?

我试图用一些code像

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;

     }

但它是最令人沮丧的(可怕的错误倾向和最令人沮丧的) 我真的厌倦了这种debuging昨天一整天和我需要找到 也许有些工作code,而不是尝试调试这个

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或反向)
  2. 创建左,右缓冲
    • 地址是y坐标
    • 的X位置的数组
    • ,如果需要还的颜色数组,纹理坐标,...
    • 对于初学者的 INT buf_x0 [YS],buf_x1 [YS]; 其中YS是屏幕的y分辨率
  1. order your points to match winding rule (so there are lines AB BC CD DA or reverse)
  2. create left and right buffer
    • 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]; where ys is screen y-resolution
  • 的,而是抽签筛选只是储存×像素的坐标来缓冲
  • 而不是:与setPixel(X,Y,颜色);
  • 完成:buf_x [Y] = X;
  • 哪个缓冲器是目的地取决于线路Y方向
  • 如果DY℃,然后填写buff_x0
  • 如果DY> 0,则填写buff_x1
  • 如果DY == 0,那么buf_x0 [Y] = MIN(x)和buf_x1 [Y] = MAX(X)
  • 在这个缓冲区包含开始和结束你的水平线条X位置

广告(5)code:

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)

图片说明:

  • 垂直矩形重新presents边境缓冲区buf_x0 [],buf_x1 []
  • 在顺时针缠绕规则,确保目标缓冲区
    • 如果其codeD妥善比buf_x0 [Y]&LT; = buf_x1 [Y]
    • ,以便绘制水平线colapses以单曲
    • 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 the draw horizontal line colapses to single for

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

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