CG Programs
CG Programs
h>
#include <stdlib.h>
#include <GL/glut.h>
int R1,R2;
void init() {
glClearColor(0.25,05,1,1);
glClear(GL_COLOR_BUFFER_BIT);
gluOrtho2D(-100,100,-100,100);
}
void midpointcircle(int R) {
double d = 5.0/4.0-R;
int x = 0;
int y = R;
circlepoints(x,y);
while(x<y) {
if(d<0){
d = d+(2*x)+3;
x++;
} else {
d = d+2*(x-y)+5;
x++;
y--;
}
circlepoints(x,y);
}
}
void display() {
midpointcircle(R1);
midpointcircle(R2);
glFlush();
}
int main(int argc,char *argv[])
{
printf("Enter the radius of circle 1: ");
scanf("%d",&R1);
printf("Enter the radius of circle 2: ");
scanf("%d",&R2);
glutInit(&argc,argv);
glutInitWindowSize(400,400);
glutInitWindowPosition(400,200);
glutCreateWindow("Mid Point Circle");
init();
display();
glutMainLoop();
return 0;
}
################
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
int x0,y0,x1,y1;
void init() {
glClearColor(0.25,05,1,1);
glClear(GL_COLOR_BUFFER_BIT);
gluOrtho2D(0,300,0,300);
}
void midpoint() {
int dx = x1-x0;
int dy = y1-y0;
int incE = dy;
int incNE = dy-dx;
int d = 2*dy-dx;
int x = x0;
int y = y0;
plot(x,y);
while(x<x1){
if(d<=0){
d=d+2*incE;
x++;
}else{
d=d+2*incNE;
x++;
y++;
}
plot(x,y);
}
}
void display() {
midpoint();
glFlush();
}
################
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#define top 8
#define bottom 4
#define right 2
#define left 1
#define true 1
#define false 0
int xmin,ymin,xmax,ymax;
double x0,y0,x1,y1;
void cohen_sutherland(){
int outcode0,outcode1,outcode,accept,done;
double x,y;
outcode0 = compute_code(x0,y0);
outcode1 = compute_code(x1,y1);
accept = false;
done = false;
do{
if((outcode0|outcode1)==0){
accept=true;
done=true;
}
else if((outcode0&outcode1)!=0)
done=true;
else{
if(outcode0)
outcode=outcode0;
else
outcode=outcode1;
if(outcode & top){
y = ymax;
x = x0 + (x1-x0)*(y-y0)/(y1-y0);
}
else if(outcode & bottom){
y = ymin;
x = x0 + (x1-x0)*(y-y0)/(y1-y0);
}
else if(outcode & right){
x = xmax;
y = y0 + (y1-y0)*(x-x0)/(x1-x0);
}
else{
x = xmin;
y = y0 + (y1-y0)*(x-x0)/(x1-x0);
}
if(outcode==outcode0){
x0=x;
y0=y;
outcode0 = compute_code(x0,y0);
}
else{
x1=x;
y1=y;
outcode1 = compute_code(x1,y1);
}
}
}while(!done);
glColor3f(1,0,0);
draw_rectangle(xmin+250,ymin,xmax+250,ymax);
if(accept){
glColor3f(0,1,0);
draw_line(x0+250,x1+250,y0,y1);
}
}
void init(){
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
gluOrtho2D(0,600,0,300);
}
void display(){
glColor3f(1,0,0);
draw_rectangle(xmin,ymin,xmax,ymax);
glColor3f(0,0,1);
draw_line(x0,x1,y0,y1);
cohen_sutherland();
glFlush();
}
################
################
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
int le[500],re[500];
void init(){
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
gluOrtho2D(0,400,0,500);
}
void display(){
draw_polygon(A,B,C,D);
polyFill(A,B,C,D);
glFlush();
}
################
################
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
#define PI 3.14159265
void display(){
draw_house(housemat);
rotate_house(housemat);
draw_house(result);
glFlush();
}
############
#include <stdio.h>
#include <stdlib.h>
#include<GL/glut.h>
#include<math.h>
float housemat[9][3]={{100,100,1},
{300,100,1},
{300,300,1},
{100,300,1},
{200,400,1},
{175,100,1},
{175,200,1},
{225,200,1},
{225,100,1}};
float result[9][3];
float xp=100,yp=100;
int degree=30;
}
void init()
{
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
gluOrtho2D(0,500,0,500);
}
void display(){
glColor3f(1,0,0);
drawhouse(housemat);
rotatehouse(housemat);
drawhouse(result);
glFlush();
}
int xmax,ymax,incx,incy,x0,y0;
int x[100],y[100];
void mesh_points(){
int i;
for(i=0;i<xmax;i++)
x[i]=x0+(i*incx);
for(i=0;i<ymax;i++)
y[i]=y0+(i*incy);
}
void draw_mesh(){
int i;
glColor3f(1,1,0);
for(i=0;i<xmax;i++)
draw_line(x[i],y[0],x[i],y[ymax-1]);
glColor3f(1,0,1);
for(i=0;i<ymax;i++)
draw_line(x[0],y[i],x[xmax-1],y[i]);
}
void display(){
mesh_points();
draw_mesh();
}
void init(){
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
gluOrtho2D(0,500,0,500);
}
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
float a[2]={2,2};
float b[2]={6,2};
float c[2]={4,6};
int k=6;
void display(){
divide(a,b,c,k);
glFlush();
}
void init(){
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
gluOrtho2D(0,8,0,8);
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitWindowSize(900,900);
glutCreateWindow("House Rotation");
init();
display();
glutMainLoop();
return 0;
}
################
################
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
int cube[8][3] = { {-1,-1,-1}, {1,-1,-1}, {1,1,-1}, {-1,1,-1}, {-1,-1,1}, {1,-1,1}, {1,1,1}, {-1,1,1} };
int theta[3] = {0,0,0};
int axis = 0;
void init(){
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glOrtho(-3,3,-3,3,3,-3);
glMatrixMode(GL_MODELVIEW);
}
void drawcube(){
// far
glColor3f(1,0,0);
drawplane(0,1,2,3);
// near
glColor3f(0,1,1);
drawplane(4,5,6,7);
// left
glColor3f(0,1,0);
drawplane(0,4,7,3);
// bottom
glColor3f(0,0,1);
drawplane(0,4,5,1);
// right
glColor3f(1,1,0);
drawplane(1,5,6,2);
// top
glColor3f(1,0,1);
drawplane(3,7,6,2);
void rotate(){
glPushMatrix();
glRotatef(theta[0],1,0,0);
glRotatef(theta[1],0,1,0);
glRotatef(theta[2],0,0,1);
drawcube();
glPopMatrix();
}
void clearscreen(){
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
}
void spin(){
theta[axis]++;
if(theta[axis]>=360)
theta[axis]=0;
glutPostRedisplay();
}
void display(){
rotate();
glutSwapBuffers();
clearscreen();
}