#include<windows.h>
#include<stdlib.h>
#include<GL/glut.h>
void init(void){ glClearColor(1.0,1.0,1.0,0.0); glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0,800,0.0,800);} void setPixel(GLint x,GLint y){ glBegin(GL_POINTS); glVertex2i(x,y);
glEnd();} inline int round(const float a){
return int(a+0.5); }
void ellipseMidpoint(int xCenter, int yCenter, int Rx, int Ry)
{ int Rx2 = Rx * Rx;
int Ry2 = Ry * Ry;
int twoRx2 = 2 * Rx2;
int twoRy2 = 2 * Ry2;
int p; int x = 0; int y = Ry; int px = 0; int py = twoRx2 * y;
void ellipsePlotPoints (int, int, int, int); /* Plot the initial point in each quadrant. */
ellipsePlotPoints (xCenter, yCenter, x, y); /* Region 1 */
p = round (Ry2 - (Rx2 * Ry) + (0.25 * Rx2)); while (px < py) {
x++; px += twoRy2; if (p < 0) p += Ry2 + px;
else { y--; py -= twoRx2; p += Ry2 + px - py; } ellipsePlotPoints (xCenter, yCenter, x, y); } /* Region 2 */ p = round (Ry2 * (x+0.5) * (x+0.5) + Rx2 * (y-1) * (y-1) - Rx2 * Ry2); while (y > 0) { y--; py -= twoRx2; if (p > 0) p += Rx2 - py; else { x++; px += twoRy2; p += Rx2 - py + px; } ellipsePlotPoints (xCenter, yCenter, x, y); }} void ellipsePlotPoints(int xCenter, int yCenter, int x, int y){ setPixel(xCenter + x, yCenter + y); setPixel(xCenter - x, yCenter + y); setPixel(xCenter + x, yCenter - y); setPixel(xCenter - x, yCenter - y); }void myDraw(void){ glClear(GL_COLOR_BUFFER_BIT); ellipseMidpoint(450,470,60,50); glFlush(); } int main(int argc,char *argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); glutInitWindowSize(900,900); glutInitWindowPosition(400,400); glutCreateWindow("ellipse"); init(); glutDisplayFunc(myDraw); glutMainLoop(); return EXIT_SUCCESS; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/132992.html
標籤:疑難問題
