中点画圆,编译没有错误,为何画不出圆

天地一棵树 2010-11-06 11:10:54
#include <GL/glut.h>
class screenPt
{
private:
int x,y;
public:
screenPt(){
x=y=0;
}
void setCoords(int xCoordValue,int yCoordValue){
x=xCoordValue;
y=yCoordValue;
}
int getx() {
return x;
}
int gety() {
return y;
}
void incrementx(){
x++;
}
void decrementy(){
y--;
}

};
void init (void)
{
glClearColor (0.5, 1.0, 0.5, 0.0); // Set display-window color to white.
glMatrixMode (GL_PROJECTION); // Set projection parameters.
gluOrtho2D (10, 200.0, 10, 150.0);
}

void setPixel(int xCoord,int yCoord)
{
glBegin(GL_POINTS);
glVertex2i(xCoord,yCoord);
glEnd();
}

void circleMidpoint(int xc,int yc,int radius)
{
screenPt circPt;
int p=1-radius;
circPt.setCoords(0,radius);
void circlePlotPoints(GLint,GLint,screenPt);
circlePlotPoints(xc,yc,circPt);
while(circPt.getx()<circPt.gety()){
circPt.incrementx();
if(p<0)
p+=2*circPt.getx()+1;
else{
circPt.decrementy();
p+=2*(circPt.getx()-circPt.gety())+1;
}
circlePlotPoints(5,5,circPt);
}
}
void drawcircle (void)
{
glClear (GL_COLOR_BUFFER_BIT); // Clear display window.
glColor3f (0.0, 0.0, 1.0); // Set line segment color to red.
circleMidpoint(0,0,3);
glFlush ( ); // Process all OpenGL routines as quickly as possible.
}



void circlePlotPoints(int xc,int yc,screenPt circPt)
{
setPixel(xc+circPt.getx(),yc+circPt.gety());
setPixel(xc-circPt.getx(),yc+circPt.gety());
setPixel(xc+circPt.getx(),yc-circPt.gety());
setPixel(xc-circPt.getx(),yc-circPt.gety());
setPixel(xc+circPt.gety(),yc+circPt.getx());
setPixel(xc+circPt.gety(),yc+circPt.getx());
setPixel(xc+circPt.gety(),yc-circPt.getx());
setPixel(xc+circPt.gety(),yc-circPt.getx());
}
void main (int argc, char** argv)
{

glutInit (&argc, argv); // Initialize GLUT.
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Set display mode.
glutInitWindowPosition (50, 100); // Set top-left display-window position.
glutInitWindowSize (400, 300); // Set display-window width and height.
glutCreateWindow ("An Example OpenGL Program"); // Create display window.
init (); // Execute initialization procedure.
glutDisplayFunc (drawcircle); // Send graphics to display window.
glutMainLoop ( ); // Display everything and wait.
}
...全文
108 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
logiciel 2010-11-06
  • 打赏
  • 举报
回复
circlePlotPoints中计算有错。以下修改可以看到一个近似的圆:

#include <stdio.h>
#include <GL/glut.h>
class screenPt
{
private:
int x,y;
public:
screenPt(){
x=y=0;
}
void setCoords(int xCoordValue,int yCoordValue){
x=xCoordValue;
y=yCoordValue;
}
int getx() {
return x;
}
int gety() {
return y;
}
void incrementx(){
x++;
}
void decrementy(){
y--;
}

};
void init (void)
{
glClearColor (0.5, 1.0, 0.5, 0.0); // Set display-window color to white.
glMatrixMode (GL_PROJECTION); // Set projection parameters.
gluOrtho2D (10, 200.0, 10, 150.0);
}

void setPixel(int xCoord,int yCoord)
{
glBegin(GL_POINTS);
glVertex2f((float)xCoord/5.0,(float)yCoord/5.0);//缩小 glVertex2i(xCoord,yCoord);
glEnd();
}

void circleMidpoint(int xc,int yc,int radius)
{
screenPt circPt;
int p=1-radius;
circPt.setCoords(0,radius);
void circlePlotPoints(GLint,GLint,screenPt);
circlePlotPoints(xc,yc,circPt);
while(circPt.getx()<circPt.gety()){
circPt.incrementx();
if(p<0)
p+=2*circPt.getx()+1;
else{
circPt.decrementy();
p+=2*(circPt.getx()-circPt.gety())+1;
}
circlePlotPoints(xc,yc,circPt);//错circlePlotPoints(5,5,circPt);

}
}
void drawcircle (void)
{
glClear (GL_COLOR_BUFFER_BIT); // Clear display window.
glColor3f (0.0, 0.0, 1.0); // Set line segment color to red.
circleMidpoint(0,0,3);
glFlush ( ); // Process all OpenGL routines as quickly as possible.
}

void circlePlotPoints(int xc,int yc,screenPt circPt)
{
setPixel(xc+circPt.getx(),yc+circPt.gety());
setPixel(xc-circPt.getx(),yc+circPt.gety());
setPixel(xc+circPt.getx(),yc-circPt.gety());
setPixel(xc-circPt.getx(),yc-circPt.gety());
setPixel(xc+circPt.gety(),yc+circPt.getx());
setPixel(xc-circPt.gety(),yc+circPt.getx());//错setPixel(xc+circPt.gety(),yc+circPt.getx());
setPixel(xc+circPt.gety(),yc-circPt.getx());
setPixel(xc-circPt.gety(),yc-circPt.getx());//错setPixel(xc+circPt.gety(),yc-circPt.getx());
}
void main (int argc, char** argv)
{

glutInit (&argc, argv); // Initialize GLUT.
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Set display mode.
glutInitWindowPosition (50, 100); // Set top-left display-window position.
glutInitWindowSize (400, 300); // Set display-window width and height.
glutCreateWindow ("An Example OpenGL Program"); // Create display window.
//不能用 init (); // Execute initialization procedure.
glutDisplayFunc (drawcircle); // Send graphics to display window.
glutMainLoop ( ); // Display everything and wait.
}
ryfdizuo 2010-11-06
  • 打赏
  • 举报
回复
opengl看不到结果,最可能就是矩阵设置有问题。投影或者照相机、
很明显你属于前者
ryfdizuo 2010-11-06
  • 打赏
  • 举报
回复
#include <GL/glut.h>
class screenPt
{
private:
int x,y;
public:
screenPt(){
x=y=0;
}
void setCoords(int xCoordValue,int yCoordValue){
x=xCoordValue;
y=yCoordValue;
}
int getx() {
return x;
}
int gety() {
return y;
}
void incrementx(){
x++;
}
void decrementy(){
y--;
}

};
void init (void)
{
//glClearColor (0.5, 1.0, 0.5, 0.0); // Set display-window color to white.
glClearColor(0, 0, 0, 0);
glMatrixMode (GL_PROJECTION); // Set projection parameters.

// 正交投影到窗口,原点位于窗口左下角
gluOrtho2D (0, 200.0, 0, 150.0);
}

void setPixel(int xCoord,int yCoord)
{
glBegin(GL_POINTS);
glVertex2i(xCoord,yCoord);
glEnd();
}

void circleMidpoint(int xc,int yc,int radius)
{
screenPt circPt;
int p=1-radius;
circPt.setCoords(0,radius);
void circlePlotPoints(GLint,GLint,screenPt);
circlePlotPoints(xc,yc,circPt);
while(circPt.getx()<circPt.gety()){
circPt.incrementx();
if(p<0)
p+=2*circPt.getx()+1;
else{
circPt.decrementy();
p+=2*(circPt.getx()-circPt.gety())+1;
}
circlePlotPoints(5,5,circPt);
}
}
void drawcircle (void)
{
glClear (GL_COLOR_BUFFER_BIT); // Clear display window.
glColor3f (0.0, 0.0, 1.0); // Set line segment color to red.

circleMidpoint(0,0,30); //半径稍微大点,才能看到~
//glRecti(0, 0, 100, 100);

glFlush ( ); // Process all OpenGL routines as quickly as possible.
}



void circlePlotPoints(int xc,int yc,screenPt circPt)
{
setPixel(xc+circPt.getx(),yc+circPt.gety());
setPixel(xc-circPt.getx(),yc+circPt.gety());
setPixel(xc+circPt.getx(),yc-circPt.gety());
setPixel(xc-circPt.getx(),yc-circPt.gety());
setPixel(xc+circPt.gety(),yc+circPt.getx());
setPixel(xc+circPt.gety(),yc+circPt.getx());
setPixel(xc+circPt.gety(),yc-circPt.getx());
setPixel(xc+circPt.gety(),yc-circPt.getx());
}

void main (int argc, char** argv)
{
glutInit (&argc, argv); // Initialize GLUT.
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Set display mode.
glutInitWindowPosition (50, 100); // Set top-left display-window position.
glutInitWindowSize (400, 300); // Set display-window width and height.
glutCreateWindow ("An Example OpenGL Program"); // Create display window.
init (); // Execute initialization procedure.
glutDisplayFunc (drawcircle); // Send graphics to display window.
glutMainLoop ( ); // Display everything and wait.
}

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧