c++ - 在 C 中生成“pieslice”而不使用 graphics.h 的 pieslice()
问题描述
在 BGI 库的“graphics.h”头文件中,该头文件中有一个函数pieslice,其语法为:
#include <graphics.h>
void pieslice(int x, int y, int stangle, int endangle, int radius);
[x,y 是圆心,stangle 和 endangle 分别是起始角和结束角]
我们可以在不使用 BGI 库的这个内置函数的情况下用 C/C++ 制作一个 pieslice。请帮助。尝试在线条和中点圆生成算法的帮助下制作它。
到目前为止我的代码:
#include<stdio.h>
#include<graphics.h>
static const double PI =3.141592
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
int xc,yc,r,st_angle,ed_angle,k;
printf("Enter the centers of pieslice:\n");
scanf("%d %d",&xc,&yc);
printf("Enter the radius:\n");
scanf("%d",&r);
printf("Enter the starting angle:\n");
scanf("%d",&st_angle);
printf("Enter the end angle:\n");
scanf("%d",&ed_angle);
for(k=st_angle; k<=ed_angle;k++)
{
double radians =(PI /180.0) * k;
int X = xc+ cos(radians) * r;
int Y = yc+ sin(radians) * r;
putpixel(x,y,WHITE);
delay(5000);
}
void wait_for_char()
{
//Wait for a key press
int in = 0;
while (in == 0) {
in = getchar();
}
}
getch();
}
我能够进行使用圆参数方程的计算部分,但无法使用该graphics.h
函数生成图形。一些帮助会很好。先感谢您。
运行此程序时,我收到此错误:
[xcb] Unknown sequence number while processing queue
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
a.out: ../../src/xcb_io.c:274: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost' failed.
[xcb] Unknown sequence number while processing queue
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
a.out: ../../src/xcb_io.c:274: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost' failed.
Aborted (core dumped)
解决方案
为什么不使用向量?
所以(0,0)
中心饼的半径r
由下式确定:
u = (cos(a0),sin(a0))
v = (cos(a1),sin(a1))
x^2 + y^2 <= r^2 // circle
(x,y) x u -> CW
(x,y) x v -> CCW
CW/CCW 是通过计算 3D 叉积并检查结果 z 坐标的符号来确定的...
因此处理圆形外接正方形中的所有像素并渲染符合所有 3 个条件的所有像素。
像这样的东西:
void pie(int x0,int y0,int r,int a0,int a1,DWORD c)
{
// variables
int x, y, // circle centered point
xx,yy,rr, // x^2,y^2,r^2
ux,uy, // u
vx,vy, // v
sx,sy; // pixel position
// my Pixel access (remove these 3 lines)
int **Pixels=Main->pyx; // Pixels[y][x]
int xs=Main->xs; // resolution
int ys=Main->ys;
// init variables
rr=r*r;
ux=double(r)*cos(double(a0)*M_PI/180.0);
uy=double(r)*sin(double(a0)*M_PI/180.0);
vx=double(r)*cos(double(a1)*M_PI/180.0);
vy=double(r)*sin(double(a1)*M_PI/180.0);
// render |<-- remove these -->|
for (y=-r,yy=y*y,sy=y0+y;y<=+r;y++,yy=y*y,sy++) if ((sy>=0)&&(sy<ys))
for (x=-r,xx=x*x,sx=x0+x;x<=+r;x++,xx=x*x,sx++) if ((sx>=0)&&(sx<xs))
if (xx+yy<=rr) // inside circle
if ((x*uy)-(y*ux)<=0) // x,y is above a0 in clockwise direction
if ((x*vy)-(y*vx)>=0) // x,y is below a1 in counter clockwise direction
Pixels[sy][sx]=c; // change for putpixel
}
但是我不使用 BGI,所以只需Pixels[sy][sx]=c;
使用您的更改putpixel(sx,sy,c);
并删除过时的范围检查 ifs for sx,sy
. 同时删除分辨率xs,ys
和Pixels
变量。
这里预览(xs2,ys2
是我的屏幕中间):
pie(xs2,ys2,ys2-200,10,50,0x00FF0000);
请注意,我有 32 位 RGB 颜色而不是您的索引 8 位颜色,角度以度为单位。另请注意,我的 y 轴指向下方,因此增量角度从 x 轴开始顺时针方向(指向右侧)
然而,这仅适用于 180 度以下的馅饼。对于较大的,您需要反转叉积条件以在不在未填充的饼图部分内时进行渲染,而不是像这样:
void pie(int x0,int y0,int r,int a0,int a1,DWORD c) // a0 < a1
{
// variables
int x, y, // circle centered point
xx,yy,rr, // x^2,y^2,r^2
ux,uy, // u
vx,vy, // v
sx,sy; // pixel position
// my Pixel access
int **Pixels=Main->pyx; // Pixels[y][x]
int xs=Main->xs; // resolution
int ys=Main->ys;
// init variables
rr=r*r;
ux=double(r)*cos(double(a0)*M_PI/180.0);
uy=double(r)*sin(double(a0)*M_PI/180.0);
vx=double(r)*cos(double(a1)*M_PI/180.0);
vy=double(r)*sin(double(a1)*M_PI/180.0);
// handle big/small pies
x=a1-a0;
if (x<0) x=-x;
// render small pies
if (x<180)
{
for (y=-r,yy=y*y,sy=y0+y;y<=+r;y++,yy=y*y,sy++) if ((sy>=0)&&(sy<ys))
for (x=-r,xx=x*x,sx=x0+x;x<=+r;x++,xx=x*x,sx++) if ((sx>=0)&&(sx<xs))
if (xx+yy<=rr) // inside circle
if (((x*uy)-(y*ux)<=0) // x,y is above a0 in clockwise direction
&&((x*vy)-(y*vx)>=0)) // x,y is below a1 in counter clockwise direction
Pixels[sy][sx]=c;
}
else{
for (y=-r,yy=y*y,sy=y0+y;y<=+r;y++,yy=y*y,sy++) if ((sy>=0)&&(sy<ys))
for (x=-r,xx=x*x,sx=x0+x;x<=+r;x++,xx=x*x,sx++) if ((sx>=0)&&(sx<xs))
if (xx+yy<=rr) // inside circle
if (((x*uy)-(y*ux)<=0) // x,y is above a0 in clockwise direction
||((x*vy)-(y*vx)>=0)) // x,y is below a1 in counter clockwise direction
Pixels[sy][sx]=c;
}
}
pie(xs2,ys2,ys2-200,50,340,0x00FF0000);
代码可以进一步优化,例如x*uy
可以更改为 for 循环中的加法,例如for(...,xuy=x*uy;...;...,xuy+=uy)
消除内部循环中的慢乘法。叉积条件下的所有 4 个therm 也是如此。
[edit1]为了更清楚,我们有这样的事情:
for (x=-r,xx=x*x,sx=x0+x;x<=+r;x++,xx=x*x,sx++)
{
if (...(x*uy)...) { do something }
}
是(x*uy)
在 的每次迭代中计算的x
。x
是递增的,因此我们可以从不需要乘法(x*uy)
的前一个值计算 的值,就像上次迭代的值一样。因此,添加包含它的单个变量可以摆脱重复乘法:((x-1)*uy)+uy
((x-1)*uy)
int xuy; // ******** *******
for (x=-r,xx=x*x,sx=x0+x,xuy=x*uy;x<=+r;x++,xx=x*x,sx++,xuy+=uy)
{
if (...(xuy)...) { do something }
}
所以最初的乘法只进行一次,然后它只是加法......
这种渲染方式也是完全可并行的......
推荐阅读
- amazon-web-services - 在 Terraform 中使用 Count 创建启动配置
- apache-storm - 如何在我的storm jar中使用acl访问kafka?
- html - 文本框的背景颜色不会改变
- laravel - 在 laravel 中添加手动数据以形成请求
- ffmpeg - 连接后过滤文本消失,为什么?
- php - php如何在数组值中找到小于数字的元素的索引?
- scala - 如何在 Scala/Spark 中使用字符串作为表达式/参数?
- reactjs - 重用 React 组件会复制 DOM 元素
- javascript - 检测网页是否正在使用谷歌分析
- javascript - redux-actions 库导出的动作类型无法重用