1. 程式人生 > >OpenGL中點演算法畫圓弧、內接多邊形演算法畫圓

OpenGL中點演算法畫圓弧、內接多邊形演算法畫圓

#include "stdafx.h"
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define PI acos(-1.0)
#define Jiao (36.0/180*PI)
void init1(){
      glClearColor(0.0,0.0,0.0,1.0);
}
double x[110],y[110];
void Set(int x,int y,int c1,int c2,int c3){  
    glVertex2f(x,y);  
}  
//中點演算法畫圓弧
void drawB(){
	int x=-50,y=50;
	double r=50;
	int H0=5-4*r;
	int xi=x,yi=y;
	int pian=200;
	glBegin(GL_POINTS);
		Set(xi+pian,yi+pian,1,0,0);
		while(xi<=x+r){
			if(H0<0){
				H0+=8*xi+12;
			} else {
				H0+=8*(xi-yi)+20;
			}
			if(H0>0)yi--;
			xi++;
			Set(xi+pian,yi+pian,1,0,0);
			
		}
	glEnd();
}
//內接多邊形逼近畫圓
void CALLBACK drawA(){
	glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1,0,0); 
	drawB();
    double xi=100,yi=100,r=50,a=18.0/180*PI,zen=2*26.0/180*PI;
    double tmp=a;
    int i;
	int n=2*PI/zen+1;
    for(i=0;i<n;i++){
        x[i]=xi+r*cos(tmp);
        y[i]=yi+r*sin(tmp);
        tmp+=zen;
    }
    x[n]=x[0],y[n]=y[0];
    for(i=0;i<n;i++){
        glBegin(GL_LINES);
           glVertex2f(x[i],y[i]);
           glVertex2f(x[i+1],y[i+1]);
        glEnd();
    }
    glFlush();
}
int main(int argc, char* argv[]){
    auxInitDisplayMode(AUX_SINGLE|AUX_RGBA);
    auxInitPosition(100,100,500,500);
    auxInitWindow("CGOpenGL");
    init1();
    auxMainLoop(drawA);
    return 0;
}