- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在考虑光栅化三角算法。 ( triangle_rasterization_lesson )
我写了下面的代码:
void triangle(int xa, int ya, int xb, int yb, int xc, int yc, TGAImage &image, TGAColor color)
{
line(xa, ya, xb, yb, image, color);
line(xa, ya, xc, yc, image, color);
line(xb, yb, xc, yc, image, color);
for (int x = xa; x<=xb; x++)
{
for (int y = ya; y<=yb; y++)
{
line(xc, yc, x, y, image, white);
}
}
}
使用 triangle(100, 100, 100, 400, 400, 100, image, red);
可以正常工作。但是,如果我交换 X(xa, ya) 和 Z(xc, yc) 坐标,则不会填满我的方 block 。
使用 triangle(70, 50, 200, 100, 20, 150, image, red);
它绘制三角形,但填充 comes out of bounds.
问题出在哪里?
最佳答案
如果它有一点帮助,这是我在 VCL/GDI 中三角形的古老 C++ 源代码:
//---------------------------------------------------------------------------
class gfx_main
{
public:
Graphics::TBitmap *bmp;
int **pyx,xs,ys;
gfx_main();
~gfx_main();
void resize(int _xs=-1,int _ys=-1);
void troj(int x0,int y0,int x1,int y1,int x2,int y2,int col); // this is filled triangle
void _troj_line(int *pl,int *pr,int x0,int y0,int x1,int y1); // this is just subroutine
};
//---------------------------------------------------------------------------
gfx_main::gfx_main()
{
bmp=new Graphics::TBitmap;
pyx=NULL;
resize(1,1);
}
//---------------------------------------------------------------------------
gfx_main::~gfx_main()
{
delete bmp;
if (pyx) delete[] pyx;
}
//---------------------------------------------------------------------------
void gfx_main::resize(int _xs,int _ys)
{
if (pyx) delete[] pyx;
if ((_xs>0)&&(_ys>0)) { bmp->Width=_xs; bmp->Height=_ys; }
xs=bmp->Width;
ys=bmp->Height;
bmp->HandleType=bmDIB;
bmp->PixelFormat=pf32bit;
pyx=new int*[ys];
for (int y=0;y<ys;y++) pyx[y]=(int*)bmp->ScanLine[y];
}
//---------------------------------------------------------------------------
//--- rasterisations: -------------------------------------------------------
//---------------------------------------------------------------------------
void gfx_main::_troj_line(int *pl,int *pr,int x0,int y0,int x1,int y1)
{
int *pp;
int x,y,kx,ky,dx,dy,k,m,p;
// DDA variables (d)abs delta,(k)step direction
kx=0; dx=x1-x0; if (dx>0) kx=+1; if (dx<0) { kx=-1; dx=-dx; }
ky=0; dy=y1-y0; if (dy>0) ky=+1; if (dy<0) { ky=-1; dy=-dy; }
// target buffer according to ky direction
if (ky>0) pp=pl; else pp=pr;
// integer DDA line start point
x=x0; y=y0;
// fix endpoints just to be sure (wrong division constants by +/-1 can cause that last point is missing)
pp[y1]=x1; pp[y0]=x0;
if (dx>=dy) // x axis is major
{
k=dy+dy;
m=(dy-dx); m+=m;
p=m;
for (;;)
{
pp[y]=x;
if (x==x1) break;
x+=kx;
if (p>0) { y+=ky; p+=m; } else p+=k;
}
}
else{ // y axis is major
k=dx+dx;
m=(dx-dy); m+=m;
p=m;
for (;;)
{
pp[y]=x;
if (y==y1) break;
y+=ky;
if (p>0) { x+=kx; p+=m; } else p+=k;
}
}
}
//---------------------------------------------------------------------------
int rgb2bgr(int col)
{
union
{
BYTE db[4];
int dd;
} c;
BYTE q;
c.dd=col;
q=c.db[0]; c.db[0]=c.db[2]; c.db[2]=q;
return c.dd;
}
//---------------------------------------------------------------------------
void gfx_main::troj(int x0,int y0,int x1,int y1,int x2,int y2,int col)
{
col=rgb2bgr(col);
int *pl,*pr; // left/right buffers
pl=new int[ys];
pr=new int[ys];
int x,y,yy0,yy1,xx0,xx1;
// boundary line coordinates to buffers
_troj_line(pl,pr,x0,y0,x1,y1);
_troj_line(pl,pr,x1,y1,x2,y2);
_troj_line(pl,pr,x2,y2,x0,y0);
// y range
yy0=y0; if (yy0>y1) yy0=y1; if (yy0>y2) yy0=y2;
yy1=y0; if (yy1<y1) yy1=y1; if (yy1<y2) yy1=y2;
// fill with horizontal lines
for (y=yy0;y<=yy1;y++)
{
if (pl[y]<pr[y]) { xx0=pl[y]; xx1=pr[y]; }
else { xx1=pl[y]; xx0=pr[y]; }
for (x=xx0;x<=xx1;x++)
pyx[y][x]=col;
}
delete[] pl;
delete[] pr;
}
//---------------------------------------------------------------------------
示例用法:
// init
gfx_main gfx;
gfx.resize(640,480);
// clear screen
TCanvas *scr=gfx.bmp->Canvas;
scr->Pen ->Color=clAqua;
scr->Font ->Color=clYellow;
scr->Brush->Color=clBlack;
scr->FillRect(TRect(0,0,xs,ys));
// triangle
troj(10,10,120,60,70,100,clAqua);
// here gfx.bmp holds the rendered image ...
来源基于此:
[edit1]
如果您感兴趣,这里是它的 3D 端口(带深度缓冲):
关于填充三角形的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39038505/
SDL3 提供了 SDL_RenderGeometry 函数绘制几何图形,用法和 OpenGL 差不多,先定义顶点数据,然后根据顶点数据绘制几何图形。 绘制三角形的代码如下: std::array
我想在图像上使用三角形类型按钮,但我无法执行此操作... 如何做到这一点? 最佳答案 这个project可以帮你。您可以自定义 UIButton 的形状。 关于iphone - 定制非矩形按钮 三角形
我一直在尝试找出如何使用 Python 制作彩虹三角螺旋。我可以制作一个方形螺旋,但它不会导入颜色。而且它不使用三角形。 输出应该是什么样子: 我取得的成就: 我的代码: import tur
我正在使用 this 研究三角形检测算法文章。我编写了这段代码,但不幸的是,当三角形之间存在交集时,该方法返回 false。 private boolean checkTriangleCollisio
我在资源文件中找到了几个关于如何在 Android 中绘制三角形的答案。但是我没有找到任何可以解释如何更改三角形旋转的内容。 我找到的例子:
对于编码类(class)中的作业,我应该找到一种方法让 Python 制作星号三角形,如下所示: x xx xxx 但是,无论我用我的代码做什么,我都无法做到这一点。最好的我可以得到的是:
我在绘制两个多边形时遇到问题。我想填充两个三角形,但一个大于第二个。我在 winforms 中使用 UserControl。代码: Point[] DOWN = new Point[] {new Po
如何测试三角形和正方形是否相交? 当我们知道它是正方形而不是矩形时,有什么方法可以优化它吗?此外,正方形是轴对齐的,这样应该可以进一步提升性能? 或者我应该把正方形分成三角形,然后对三角形-三角形相交
我有一个方法是画一个多边形,然后将多边形向右旋转90度,使其原来的顶点现在指向右边。 这是绘制多边形(三角形)的代码,但我不知道如何旋转它。 Point[] points = new Point[3]
我知道有高效的多边形裁剪算法(例如 Maillot、Vatti、Greiner-Hormann)。然而,这些算法适用于任意多边形,尽管它们适合我的情况,但在我看来,对像我这样的简单情况使用这种通用算法
我的问题可能很愚蠢,但我没有找到三角形 strip 使用的好例子: http://i.stack.imgur.com/KL8jk.png 像这样的顶点: A: -0.5f, -0.5f, // Bo
我正在尝试创建一个等边三角形,您可以在 fiddle 中看到它: 我的想法是,我将笔放在 (0, 0) 处,然后在 (20, 11) 处画线,但三角形看起来不正确。 最佳答案 您的三角形已被
通过编写一些逻辑代码,只是无法弄清楚如何以所需的形式获得 01 三角形的输出,三角形确实打印出来,但不是根据要求的输出。 import java.util.Scanner; import java
我一直在尝试制作一个简单的 pygame 程序来检查光标是否在三角形内部或外部。我通过找到较大三角形的面积,然后从鼠标位置到所有三个点制作三个内部三角形并找到它们的面积来完成此操作。 根据我的理解,如
我有一个方法 drawTriangle,它在 JAVA 中的 OpenGL 程序的 display() 方法中被引用。 public void drawTriangle(GL gl, int x1,
我正在尝试用 C++ 创建一个程序,该程序将数字的三角形模式放入二维数组中。 示例: 1 3 4 5 9 2 9 4 6 1 顶行是一个数字(整数),三角形的每一行比它上面的行多一
所以我最近一直在尝试学习 OpenGL,遵循了几个文本和视频教程。 我无法绘制三角形,我已经双重和三次检查我是否以正确的顺序执行了所有必要的步骤,但我显然遗漏了一些东西 在添加一些代码之前,我应该声明
我遇到了一个用递归绘制谢尔宾斯基三角形的程序。我如何解释这段代码是调用 sierpinski1 直到 n == 0,然后只绘制 3 个小三角形(每次调用一个三角形),因为 n == 0 是绘制某些东西
我有一个需要 3 个点的函数,我将使用这些点来绘制一个三角形,就好像我在使用 glVertex 函数一样。 但由于我想在避免透视变形的同时对这个三角形进行纹理贴图,我必须对其进行 segmentati
下面的代码应该为三角形添加一个 3d 对象,但我收到错误 Assets/Scripts/MakeTriangle.cs(6,28):错误 CS0120:需要对象引用才能访问非静态成员 `UnityEn
我是一名优秀的程序员,十分优秀!