问题描述
我需要用 c (openGL) 画一个正方形,我只有 1 个坐标,它是正方形的中心(比方说 0.5,0.5),我需要画一个正方形 ABCD,每边长 0.2(AB、BC、CD、DA),我尝试使用下一个函数,但它没有绘制任何内容,
i need to draw a square using c (openGL),i only have 1 coordinate which is the center of the square (lets say 0.5,0.5) and i need to draw a square ABCD with each side 0.2 length (AB,BC,CD,DA),I tried using the next function but it does not draw anything for some reson,
void drawSquare(double x1,double y1,double radius)
{
glColor3d(0,0,0);
glBegin(GL_POLYGON);
double locationX = x1;
double locationY = x2;
double r = radius;
for(double i=0; i <= 360 ; i+=0.1)
{
glVertex2d(locationX + radius*i, locationY + radius*i);
}
glEnd();
}
有人可以告诉我为什么它不起作用\指向我正确的方向(我不想绘制具有 4 个正常坐标的多边形,但只有 1 个坐标具有给定的半径,谢谢!
can someone please tell me why its not working\point me to the right direction (i do not want to draw polygon with 4 coordinated normally, but with only 1 coordinate with a givven radius,thanks!
推荐答案
您的代码甚至不会画圆.如果有的话,它会很快绘制一条延伸出视图区域的对角线.圆图需要根据半径和角度使用正弦和余弦.
Your code will not even draw a circle. If anything, it will draw a diagonal line extending out of the view area very quickly. A circle plot would need to use sine and cosine, based on the radius and angle.
这个代码我没试过,但是需要更像这样才能画个正方形.
I have not tried this code, but it needs to be more like this to draw a square.
void drawSquare(double x1, double y1, double sidelength)
{
double halfside = sidelength / 2;
glColor3d(0,0,0);
glBegin(GL_POLYGON);
glVertex2d(x1 + halfside, y1 + halfside);
glVertex2d(x1 + halfside, y1 - halfside);
glVertex2d(x1 - halfside, y1 - halfside);
glVertex2d(x1 - halfside, y1 + halfside);
glEnd();
}
没有定义法线:也许我应该逆时针走.
There are no normals defined: perhaps I should have travelled counter-clockwise.
这篇关于openGL(c) 画正方形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!