0% found this document useful (0 votes)
52 views

CG Programs

This document contains C code to rotate a house shape about a point using 2D matrix transformations. The code defines a house shape as 9 points in a 2D matrix. It draws the original house shape. It then defines a 2D rotation matrix based on the degree of rotation and the rotation point. It multiplies the house point matrix by the rotation matrix to calculate the rotated coordinates. Finally it redraws the house shape using the rotated coordinates.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

CG Programs

This document contains C code to rotate a house shape about a point using 2D matrix transformations. The code defines a house shape as 9 points in a 2D matrix. It draws the original house shape. It then defines a 2D rotation matrix based on the degree of rotation and the rotation point. It multiplies the house point matrix by the rotation matrix to calculate the rotated coordinates. Finally it redraws the house shape using the rotated coordinates.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

#include <stdio.

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 plot(int x,int y) {


glColor3f(1,0,0);
glPointSize(3);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}

void circlepoints(int x,int y) {


plot(x,y);
plot(x,-y);
plot(-x,y);
plot(-x,-y);
plot(y,x);
plot(y,-x);
plot(-y,x);
plot(-y,-x);
}

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 plot(int x,int y) {


glColor3f(1,0,0);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}

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();
}

int main(int argc,char *argv[])


{
printf("Enter the starting point coordinate: ");
scanf("%d%d",&x0,&y0);
printf("Enter the ending point coordinate: ");
scanf("%d%d",&x1,&y1);
glutInit(&argc,argv);
glutInitWindowSize(400,400);
glutInitWindowPosition(400,200);
glutCreateWindow("Basic Line");
init();
display();
glutMainLoop();
return 0;
}

################

#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 draw_rectangle(int xmin,int ymin, int xmax, int ymax){


glBegin(GL_LINE_LOOP);
glVertex2i(xmin,ymin);
glVertex2i(xmax,ymin);
glVertex2i(xmax,ymax);
glVertex2i(xmin,ymax);
glEnd();
}

void draw_line(double x0,double x1,double y0,double y1){


glBegin(GL_LINES);
glVertex2d(x0,y0);
glVertex2d(x1,y1);
glEnd();
}

int compute_code(double x,double y){


int code=0;
if(y>ymax)
code=code|top;
if(y<ymin)
code=code|bottom;
if(x>xmax)
code=code|right;
if(x<xmin)
code=code|left;
return code;
}

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();
}

int main(int argc,char *argv[])


{
printf("Enter the boundaries of clip window: (order -> xmin ymin xmax ymax)\n");
scanf("%d%d%d%d",&xmin,&ymin,&xmax,&ymax);
printf("Enter coordinates of the line: (Order -> x0 y0 x1 y1)\n");
scanf("%lf%lf%lf%lf",&x0,&y0,&x1,&y1);
glutInit(&argc,argv);
glutInitWindowSize(600,300);
glutCreateWindow("Line Clip Algorithm");
init();
display();
glutMainLoop();
return 0;
}

################

################

#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>

int A[2] = {200,200},


B[2] = {100,300},
C[2] = {200,400},
D[2] = {300,300};

int le[500],re[500];

void draw_polygon(int A[],int B[],int C[],int D[]){


glColor3f(1,0,0);
glBegin(GL_LINE_LOOP);
glVertex2iv(A);
glVertex2iv(B);
glVertex2iv(C);
glVertex2iv(D);
glEnd();
}

void arrayInit(int le[],int re[]){


int i;
for(i=0;i<500;i++){
le[i]=999;
re[i]=0;
}
}

void edgeDetect(int v0[],int v1[]){


double xmin,ymin,xmax,ymax,m,x;
int y;
if(v0[1]<v1[1]){
xmin=v0[0];
ymin=v0[1];
xmax=v1[0];
ymax=v1[1];
}
else{
xmin=v1[0];
ymin=v1[1];
xmax=v0[0];
ymax=v0[1];
}
m = (ymax-ymin)/(xmax-xmin);
x = xmin;
for(y=ymin;y<=ymax;y++){
if(x<le[y]){
le[y]=x;
}
if(x>re[y]){
re[y]=x;
}
x=x+1/m;
}
}
void fillPixels(int le[],int re[]){
int i,j;
for(i=0;i<500;i++){
if(le[i]<re[i]){
for(j=le[i];j<=re[i];j++){
glColor3f(0,1,0);
glBegin(GL_POINTS);
glVertex2i(j,i);
glEnd();
glFlush();
}
}
glFlush();
}
}

void polyFill(int A[],int B[],int C[],int D[]){


arrayInit(le,re);
edgeDetect(A,B);
edgeDetect(B,C);
edgeDetect(C,D);
edgeDetect(D,A);
fillPixels(le,re);
}

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();
}

int main(int argc,char *argv[])


{
glutInit(&argc,argv);
glutInitWindowSize(400,500);
glutCreateWindow("Filling Convex Polygon");
init();
display();
glutMainLoop();
return 0;
}

################

################
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>

#define PI 3.14159265

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;

void draw_house(float mat[9][3]){


glColor3f(0,0,1);
glBegin(GL_LINE_LOOP);
glVertex2fv(mat[0]);
glVertex2fv(mat[1]);
glVertex2fv(mat[2]);
glVertex2fv(mat[3]);
glEnd();
glColor3f(1,0,0);
glBegin(GL_LINE_STRIP);
glVertex2fv(mat[2]);
glVertex2fv(mat[4]);
glVertex2fv(mat[3]);
glEnd();
glColor3f(0,1,0);
glBegin(GL_LINE_STRIP);
glVertex2fv(mat[5]);
glVertex2fv(mat[6]);
glVertex2fv(mat[7]);
glVertex2fv(mat[8]);
glEnd();
}

void rotate_house(float mat[9][3]){


int i,j,k;
float sum = 0;
float theta = PI/180*degree;
float rotA[3][3] = {{cos(theta),sin(theta),0},{-sin(theta),cos(theta),0},{-xp*cos(theta)+yp*sin(theta)+xp,-yp
*cos(theta)+yp-xp*sin(theta),1}};
for(i=0;i<9;i++){
for(j=0;j<3;j++){
for(k=0;k<3;k++){
sum += mat[i][k]*rotA[k][j];
}
result[i][j] = sum;
sum=0;
}
}
}
void init(){
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
gluOrtho2D(-200,500,-100,500);
}

void display(){
draw_house(housemat);
rotate_house(housemat);
draw_house(result);
glFlush();
}

int main(int argc,char *argv[])


{
printf("Enter the degree of rotation: ");
scanf("%d",&degree);
glutInit(&argc,argv);
glutInitWindowSize(700,600);
glutCreateWindow("House Rotation");
init();
display();
glutMainLoop();
return 0;
}

############

#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 drawhouse(float mat[9][3]){


glColor3f(1,0,0);
glBegin(GL_LINE_LOOP);
glVertex2fv(mat[0]);
glVertex2fv(mat[1]);
glVertex2fv(mat[2]);
glVertex2fv(mat[3]);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex2fv(mat[2]);
glVertex2fv(mat[4]);
glVertex2fv(mat[3]);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex2fv(mat[5]);
glVertex2fv(mat[6]);
glVertex2fv(mat[7]);
glVertex2fv(mat[8]);
glEnd();

}
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();
}

void rotatehouse(float mat[9][3]){


float z=(3.142/180)*degree;
float ra[3][3]={{cos(z),sin(z),0},
{-sin(z),cos(z),0},
{-xp*cos(z)+yp*sin(z)+xp,-xp*sin(z)-yp*cos(z)+yp,1}};
for(int i=0;i<9;i++){
for(int j=0;j<3;j++){
result[i][j]=0;
for(int k=0;k<3;k++){
result[i][j]=result[i][j]+housemat[i][k]*ra[k][j];
}
}
}

int main(int argc,char *argv[])


{
glutInit(&argc,argv);
glutCreateWindow("test");
init();
display();
glutMainLoop();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>

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_line(int x0,int y0, int x1, int y1){


glBegin(GL_LINES);
glVertex2i(x0,y0);
glVertex2i(x1,y1);
glEnd();
glFlush();
}

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);
}

int main(int argc,char *argv[])


{
printf("Enter x0 and y0: ");
scanf("%d%d",&x0,&y0);
printf("Enter xmax and ymax: ");
scanf("%d%d",&xmax,&ymax);
printf("Enter the incX and incY: ");
scanf("%d%d",&incx,&incy);
glutInit(&argc,argv);
glutInitWindowSize(500,500);
glutCreateWindow("Rectangular Mesh");
init();
display();
glutMainLoop();
return 0;
}

#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 drawTriangle(float a[], float b[], float c[]){


glColor3f(1,1,0);
glBegin(GL_TRIANGLES);
glVertex2fv(a);
glVertex2fv(b);
glVertex2fv(c);
glEnd();
}

void divide(float a[], float b[], float c[], int k){


if(k==1){
drawTriangle(a,b,c);
return;
}
float ab[2],bc[2],ca[2];
int i;
for(i=0;i<2;i++){
ab[i]=(a[i]+b[i])/2;
bc[i]=(b[i]+c[i])/2;
ca[i]=(a[i]+c[i])/2;
}
divide(a,ab,ca,k-1);
divide(ab,b,bc,k-1);
divide(ca,bc,c,k-1);
return;
}

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 drawplane(int a,int b, int c,int d){


glBegin(GL_POLYGON);
glVertex3iv(cube[a]);
glVertex3iv(cube[b]);
glVertex3iv(cube[c]);
glVertex3iv(cube[d]);
glEnd();
}

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();
}

void keyboard_action(char ch,int x,int y){


if(ch == ’x’ || ch == ’X’)
axis = 0;
else if(ch == ’y’ || ch == ’Y’)
axis = 1;
else if(ch == ’z’ || ch == ’Z’)
axis = 2;
return;
}

int main(int argc, char *argv[])


{
glutInit(&argc,argv);
glutInitWindowSize(500,500);
glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE);
glutCreateWindow("Cube Rotation");
init();
glEnable(GL_DEPTH_TEST);
glutKeyboardFunc(keyboard_action);
glutDisplayFunc(display);
glutIdleFunc(spin);
glutMainLoop();
return 0;
}

You might also like