Practical No.
1
Aim: Write a program for 2D line drawing as Raster Graphics Display.
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int input;
int x,y,x1,y1,x2,y2,p,dx,dy;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI");
printf("\nEnter the x-coordinate of the first point :: ");
scanf("%d",&x1);
printf("\nEnter the y-coordinate of the first point :: ");
scanf("%d",&y1);
printf("\nEnter the x-coordinate of the second point :: ");
scanf("%d",&x2);
printf("\nEnter the y-coordinate of the second point :: ");
scanf("%d",&y2);
x=x1; y=y1; dx=x2-x1; dy=y2-y1;
putpixel(x,y,2);
p=((2*dy)-dx);
while(x<=x2)
{
if(p<0)
{
x=x+1; p=2*x-dx;
}
else
{
x=x+1; y=y+1; p=p+2*dy;
}
putpixel(x,y,7);
}
getch();
closegraph();
}
1
Output:
2
Practical No.2
Aim: Write a program for circle drawing as Raster Graphics Display.
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void circlepoints(int,int);
void main()
{
int x,y,p,r;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI");
clrscr();
printf("Enter the radius for the required circle : ");
scanf("%d",&r);
x=0; y=r; p=1-r;
while(x<y)
{
x++;
if(p>0)
{
p=p+2*(x-y)+1; y--;
}
else
p=p+2*x+1;
circlepoints(x,y);
}
getch();
closegraph();
}
void circlepoints(int x,int y)
{
putpixel( x+300, y+300,8);
putpixel( x+300,-y+300,8);
putpixel(-x+300, y+300,8);
putpixel(-x+300,-y+300,8);
putpixel( y+300, x+300,8);
putpixel( y+300,-x+300,8);
putpixel(-y+300, x+300,8);
putpixel(-y+300,-x+300,8);
}
3
Output:
4
Practical No.3
Aim: Write a program for polygon filling as Raster Graphics Display.
Code:
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<process.h>
void main()
{
int graphdriver=DETECT,graphmode;
initgraph(&graphdriver,&graphmode,"C:\\TURBOC3\\BGI");
int p=1,x;
int a[12]={100,100,150,150,200,100,200,200,100,200,100,100};
cout<<" Filling the Polygon drawn below\n";
delay(1000);
drawpoly(6,a);
delay(2000);
delay(500);
for(int i=100;i<200;i++)
{
p=1;
for(int j=100;j<=200;j++)
{
x=getpixel(j,i);
for(int d=0;d<11;d++)
{
if(j==a[d]&&i==a[d+1] )
break;
else
{
if(x>0&&d==10)
p++;
if(p%2==0)
putpixel(j,i,4);
}
}
}
}
getch();
closegraph();
}
5
Output:
6
Practical No.4
Aim: Write a program for line clipping.
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void storepoints(int,int,int,int,int,int,int[]);
void main()
{
int gdriver=DETECT,gmode;
int x1,x2,y1,y2,xmax,ymax,xmin,ymin,a[10],b[10],xi1,xi2,yi1,yi2,flag=0;
float m;
int i;
clrscr();
printf("output");
printf("\n");
printf("Enter the value of x1,y1,x2,y2: >");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("Enter the value of xmax,ymax,xmin,ymin:>");
scanf("%d%d%d%d",&xmax,&ymax,&xmin,&ymin);
storepoints(x2,y2,ymin,ymax,xmax,xmin,b);
for(i=1;i<=4;i++)
{
if(a[i]*b[i]==0)
flag=1;
else
flag=0;
}
if(flag==1)
{
m=(y2-y1)/(x2-x1);
xi1=x1;
yi1=y1;
}
if(a[1]==1)
{
yi1=ymax;
xi1=x1+((1/m)*(yi1-y1));
}
else
{
if(a[2]==1)
{
7
yi1=ymin;
xi1=x1+((1/m)*(yi1-y1));
}
}
if(a[3]==1)
{
xi1=xmax;
yi1=y1+(m*(xi1-x1));
}
if(a[4]==1)
{
xi1=xmin;
yi1=y1+(m*(xi1-x1));
}
else if(b[1]==1)
{
yi2=ymax;
xi2=x2+((1/m)*(yi2-y2));
}
else if(b[2]==1)
{
yi2=ymin;
xi2=x2+((1/m)*(yi2-y2));
}
else if(b[3]==1)
{
xi2=xmax;
yi2=y2+((1/m)*(xi2-x2));
}
else if(b[4]==1)
{
xi2=xmin;
yi2=y2+(m*(xi2-x2));
}
clrscr();
initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
delay(1700);
closegraph();
clrscr();
initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI");
line(xi1,yi1,xi2,yi2);
rectangle(xmin,ymin,xmax,ymax);
if(flag==0)
{
printf("\n no clipping is required");
8
}
getch();
closegraph();
}
void storepoints(int x1,int y1,int ymax,int xmax,int xmin,int ymin,int c[10])
{
if((y1-ymax)>0)
c[1]=1;
else
c[1]=0;
if((ymin-y1)>0)
c[2]=1;
else
c[2]=0;
if((x1-xmax)>0)
c[3]=1;
else
c[3]=0;
if((xmin-x1)>0)
c[4]=1;
else
c[4]=0;
}
9
Output:
10
11
Practical No.5
Aim: Write a program for polygon clipping.
Code:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#define round(a) ((int)(a+0.5))
int k;
float xmin,ymin,xmax,ymax,arr[20],m;
void clipl(float x1,float y1,float x2,float y2)
{
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;
if(x1 >= xmin && x2 >= xmin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1 < xmin && x2 >= xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1 >= xmin && x2 < xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
k+=2;
}
}
void clipt(float x1,float y1,float x2,float y2)
{
if(y2-y1)
m=(x2-x1)/(y2-y1);
else
m=100000;
if(y1 <= ymax && y2 <= ymax)
12
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1 > ymax && y2 <= ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1 <= ymax && y2 > ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
k+=2;
}
}
void clipr(float x1,float y1,float x2,float y2)
{
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;
if(x1 <= xmax && x2 <= xmax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1 > xmax && x2 <= xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1 <= xmax && x2 > xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
k+=2;
}
13
}
void clipb(float x1,float y1,float x2,float y2)
{
if(y2-y1)
m=(x2-x1)/(y2-y1);
else
m=100000;
if(y1 >= ymin && y2 >= ymin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1 < ymin && y2 >= ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1 >= ymin && y2 < ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
k+=2;
}
}
void main()
{
int gdriver=DETECT,gmode,n,poly[20];
float xi,yi,xf,yf,polyy[20];
clrscr();
cout<<"Coordinates of rectangular clip window :\nxmin,ymin :";
cin>>xmin>>ymin;
cout<<"xmax,ymax :";
cin>>xmax>>ymax;
cout<<"\n\nPolygon to be clipped :\nNumber of sides :";
cin>>n;
cout<<"Enter the coordinates :";
for(int i=0;i < 2*n;i++)
cin>>polyy[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
for(i=0;i < 2*n+2;i++)
14
poly[i]=round(polyy[i]);
initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI");
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
cout<<"\t\tUNCLIPPED POLYGON";
setcolor(WHITE);
fillpoly(n,poly);
getch();
cleardevice();
k=0;
for(i=0;i < 2*n;i+=2)
clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i < k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i < 2*n;i+=2)
clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i < k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i < 2*n;i+=2)
clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i < k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i < 2*n;i+=2)
clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
for(i=0;i < k;i++)
poly[i]=round(arr[i]);
if(k)
fillpoly(k/2,poly);
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
cout<<"\tCLIPPED POLYGON";
getch();
closegraph();
}
15
Output:
16
17
Practical No.6
Aim: Write a program for displaying 3D objects as 2D display using perspective transformation.
Code:
#include<stdio.h>
#include<math.h>
#include<graphics.h>
main()
{
int x1,y1,x2,y2,gd,gm;
int ymax,a[4][8];
float par[4][4],b[4][8];
int i,j,k,m,n,p;
int xp, yp, zp, x, y, z;
a[0][0] = 100; a[1][0] = 100; a[2][0] = -100;
a[0][1] = 200; a[1][1] = 100; a[2][1] = -100;
a[0][2] = 200; a[1][2] = 200; a[2][2] = -100;
a[0][3] = 100; a[1][3] = 200; a[2][3] = -100;
a[0][4] = 100; a[1][4] = 100; a[2][4] = -200;
a[0][5] = 200; a[1][5] = 100; a[2][5] = -200;
a[0][6] = 200; a[1][6] = 200; a[2][6] = -200;
a[0][7] = 100; a[1][7] = 200; a[2][7] = -200;
detectgraph(&gd,&gm);
initgraph(&gd,&gm, "c:\\TURBOC3\\BGI");
ymax = getmaxy();
xp = 300; yp = 320; zp = 100;
for(j=0; j<8; j++)
{
x = a[0][j]; y = a[1][j]; z = a[2][j];
b[0][j] = xp - ( (float)( x - xp )/(z - zp)) * (zp);
b[1][j] = yp - ( (float)( y - yp )/(z - zp)) * (zp);
}
/*- front plane display -*/
for(j=0;j<3;j++)
{
x1=(int) b[0][j]; y1=(int) b[1][j];
18
x2=(int) b[0][j+1]; y2=(int) b[1][j+1];
line( x1,ymax-y1,x2,ymax-y2);
}
x1=(int) b[0][3]; y1=(int) b[1][3];
x2=(int) b[0][0]; y2=(int) b[1][0];
line( x1, ymax-y1, x2, ymax-y2);
/*- back plane display -*/
setcolor(11);
for(j=4;j<7;j++)
{
x1=(int) b[0][j]; y1=(int) b[1][j];
x2=(int) b[0][j+1]; y2=(int) b[1][j+1];
line( x1, ymax-y1, x2, ymax-y2);
}
x1=(int) b[0][7]; y1=(int) b[1][7];
x2=(int) b[0][4]; y2=(int) b[1][4];
line( x1, ymax-y1, x2, ymax-y2);
setcolor(7);
for(i=0;i<4;i++)
{
x1=(int) b[0][i]; y1=(int) b[1][i];
x2=(int) b[0][4+i]; y2=(int) b[1][4+i];
line( x1, ymax-y1, x2, ymax-y2);
}
getch(); getch();
}
19
Output:
20
Practical No.7
Aim: Write a program for rotation of 3D object about arbitrary axis.
Code:
#include <stdio.h>
#include <graphics.h>
#include <math.h>
#include <stdlib.h>
#include <dos.h>
#include <conio.h>
#define ORG -50
double face1[5][2] = {
{ 250, 125 },
{ 350, 125 },
{ 350, 225 },
{ 250, 225 },
{ 250, 125 }
};
double face2[5][2] = {
{ 250+ORG, 125-ORG },
{ 350+ORG, 125-ORG },
{ 350+ORG, 225-ORG },
{ 250+ORG, 225-ORG },
{ 250+ORG, 125-ORG }
};
double angle = 5.0 * M_PI / 180;
double midx1, midy1, midx2, midy2;
void rotate (void)
{
int i;
for (i=0; i<5; i++)
{
double xnew, ynew;
xnew = midx1 + (face1[i][0] - midx1) * cos (angle) - (face1[i][1] - midy1) * sin (angle);
ynew = midy1 + (face1[i][0] - midx1) * sin (angle) + (face1[i][1] - midy1) * cos (angle);
face1[i][0] = xnew;
face1[i][1] = ynew;
xnew = midx2 + (face2[i][0] - midx2) * cos (angle) - (face2[i][1] - midy2) * sin (angle);
ynew = midy2 + (face2[i][0] - midx2) * sin (angle) + (face2[i][1] - midy2) * cos (angle);
21
face2[i][0] = xnew;
face2[i][1] = ynew;
}
cleardevice();
for (i=0; i<4; i++)
{
setcolor(7);
line (face1[i][0], face1[i][1], face1[i+1][0], face1[i+1][1]);
setcolor(8);
line (face2[i][0], face2[i][1], face2[i+1][0], face2[i+1][1]);
setcolor(9);
line (face1[i][0], face1[i][1], face2[i][0], face2[i][1]);
}
delay (125);
}
void main()
{
int gd = DETECT, gm;
midx1 = (face1[0][0] + face1[1][0]) / 2.0;
midy1 = (face1[1][1] + face1[2][1]) / 2.0;
midx2 = (face2[0][0] + face2[1][0]) / 2.0;
midy2 = (face2[1][1] + face2[2][1]) / 2.0;
initgraph (&gd, &gm, "C:\\TURBOC3\\BGI");
while (!kbhit())
rotate();
closegraph();
}
22
Output:
23
Practical No.8
Aim: Write a program to rotate a triangle about origin.
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void triangle(int x1,int y1,int x2,int y2,int x3,int y3);
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3);
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,x3,y3;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
triangle(x1,y1,x2,y2,x3,y3);
getch();
cleardevice();
Rotate(x1,y1,x2,y2,x3,y3);
setcolor(1);
triangle(x1,y1,x2,y2,x3,y3);
getch();
}
void triangle(int x1,int y1,int x2,int y2,int x3,int y3)
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3)
{
int x,y,a1,b1,a2,b2,a3,b3,p=x2,q=y2;
float Angle;
printf("Enter the angle for rotation:");
scanf("%f",&Angle);
cleardevice();
24
Angle=(Angle*3.14)/180;
a1=p+(x1-p)*cos(Angle)-(y1-q)*sin(Angle);
b1=q+(x1-p)*sin(Angle)+(y1-q)*cos(Angle);
a2=p+(x2-p)*cos(Angle)-(y2-q)*sin(Angle);
b2=q+(x2-p)*sin(Angle)+(y2-q)*cos(Angle);
a3=p+(x3-p)*cos(Angle)-(y3-q)*sin(Angle);
b3=q+(x3-p)*sin(Angle)+(y3-q)*cos(Angle);
printf("Rotate");
triangle(a1,b1,a2,b2,a3,b3);
}
25
Output:
26
Practical No.9
Aim: Write a program to implement Bezier curves for a given set of control points..
Code:
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>
void bezier (int x[4], int y[4])
{
int gd = DETECT, gm;
int i;
double t;
initgraph (&gd, &gm, "C:\\TURBOC3\\BGI");
for (t = 0.0; t < 1.0; t += 0.0005)
{
double xt = pow (1-t, 3) * x[0] + 3 * t * pow (1-t, 2) * x[1] +
3 * pow (t, 2) * (1-t) * x[2] + pow (t, 3) * x[3];
double yt = pow (1-t, 3) * y[0] + 3 * t * pow (1-t, 2) * y[1] +
3 * pow (t, 2) * (1-t) * y[2] + pow (t, 3) * y[3];
putpixel (xt, yt, WHITE);
}
for (i=0; i<4; i++)
putpixel (x[i], y[i], YELLOW);
getch();
closegraph();
}
void main()
{
int x[4], y[4];
int i;
printf ("Enter the x- and y-coordinates of the four control points.\n");
for (i=0; i<4; i++)
scanf ("%d%d", &x[i], &y[i]);
bezier (x, y);
}
27
Output:
28
Practical No.10
Aim: Write a program for Z-Buffer Algorithm.
Code:
_2Dpoint.cs
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace Graphics3Dto2D
{
class Screen
{
public _2Dpoint center;
public _2Dpoint size;
public Screen()
{
center = new _2Dpoint(720, 420);
size = new _2Dpoint(800, 800);
}
}
}
_3Dpoint.cs
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace Graphics3Dto2D
{
class _3Dpoint
{
public double x, y, z;
public _3Dpoint(double xx, double yy, double zz)
{
x = xx;
y = yy;
z = zz;
}
public _3Dpoint()
{
29
x = 0;
y = 0;
z = 0;
}
public _3Dpoint(_3Dpoint Object)
{
this.x = Object.x;
this.y = Object.y;
this.z = Object.z;
}
}
}
[Link]
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace Graphics3Dto2D
{
class Camera
{
public _3Dpoint from;
public _3Dpoint to;
public _3Dpoint up;
public double angleh, anglev;
public double zoom;
public double front, back;
public short projection;
public Camera()
{
from = new _3Dpoint(0, -50, 0);
to = new _3Dpoint(0, 50, 0);
up = new _3Dpoint(0, 0, 1);
angleh = 45.0;
anglev = 45.0;
zoom = 1.0;
front = 1.0;
back = 200.0;
projection = 0;
}
}
}
30
Coefficient_Of_Plane.cs
using System;
using [Link];
using [Link];
using [Link];
namespace Graphics3Dto2D
{
class Coefficient_Of_Plane
{
public double A, B, C, D;
public Coefficient_Of_Plane()
{
A = B = C = D = 0;
}
}
}
[Link]
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace Graphics3Dto2D
{
class DepthBuffer
{
public Coefficient_Of_Plane COPLANE;
public DepthBuffer()
{
COPLANE = new Coefficient_Of_Plane();
}
public Coefficient_Of_Plane Coefficient_Value(_3Dpoint pt1, _3Dpoint pt2, _3Dpoint pt3)
{
// double Y = 0;
Coefficient_Of_Plane pt;
pt = new Coefficient_Of_Plane();
pt.A = (pt2.z - pt3.z) * (pt1.y - pt2.y) - (pt1.z - pt2.z) * (pt2.y - pt3.y);
pt.B = (pt2.x - pt3.x) * (pt1.z - pt2.z) - (pt1.x - pt2.x) * (pt2.z - pt3.z);
pt.C = (pt2.y - pt3.y) * (pt1.x - pt2.x) - (pt1.y - pt2.y) * (pt2.x - pt3.x);
pt.D = - pt1.x * (pt2.y * pt3.z - pt2.z * pt3.y) + pt1.y * (pt2.x * pt3.z - pt2.z * pt3.x) - pt1.z * (pt2.x *
pt3.y - pt2.y * pt3.x);
31
return pt;
}
public double DepthValue(Coefficient_Of_Plane surface,int x,int y)
{
double z = 200;
if (surface.B != 0)
z = (-surface.A * x - surface.C * y - surface.D) / surface.B;
return z;
}
public bool CheckInside(Point point1, Point point2, Point point3, int x, int y)
{
float fxyC, fxy;
fxyC = point3.Y * (point2.X - point1.X) - point3.X * (point2.Y - point1.Y) + point1.X * (point2.Y -
point1.Y) - point1.Y * (point2.X - point1.X);
fxy = y * (point2.X - point1.X) - x * (point2.Y - point1.Y) + point1.X * (point2.Y - point1.Y) - point1.Y *
(point2.X - point1.X);
if (((fxyC <= 0) && (fxy <= 0)) || ((fxyC >= 0) && (fxy >= 0)))
return true;
else
return false;
}
//Subroutine to check any point(x,y) inside the triangle
public bool inside_triangle_check(Point pt1, Point pt2, Point pt3, int x, int y)
{
bool check = CheckInside(pt1, pt2, pt3, x, y) && CheckInside(pt2, pt3, pt1, x, y) && CheckInside(pt3,
pt1, pt2, x, y);
if (check == true) return true;
else return false;
}
}
}
32
[Link]
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace Graphics3Dto2D
{
class Projection
{
private _3Dpoint origin;
private _3Dpoint e1, e2, n1, n2;
private Camera camera;
private Screen screen;
private double tanthetah, tanthetav;
private _3Dpoint basisa, basisb, basisc;
private double EPSILON;
private double DTOR;// 0.01745329252
public _2Dpoint p1;
public _2Dpoint p2;
public Projection()
{
EPSILON = 0.001;
DTOR = 0.01745329252;
camera = new Camera();
screen = new Screen();
origin = new _3Dpoint();
basisa = new _3Dpoint();
basisb = new _3Dpoint();
basisc = new _3Dpoint();
p1 = new _2Dpoint();
p2 = new _2Dpoint();
e1 = new _3Dpoint();
e2 = new _3Dpoint();
n1 = new _3Dpoint();
n2 = new _3Dpoint();
if (Trans_Initialise() != true)
{
[Link]("Error in initializing variable");
}
}
public bool Trans_Initialise()
{
/* Is the camera position and view vector coincident ? */
33
if (EqualVertex([Link], [Link]))
{
return (false);
}
/* Is there a legal camera up vector ? */
if (EqualVertex([Link], origin))
{
return (false);
}
basisb.x = [Link].x - [Link].x;
basisb.y = [Link].y - [Link].y;
basisb.z = [Link].z - [Link].z;
Normalise(basisb);
basisa = CrossProduct([Link], basisb);
Normalise(basisa);
/* Are the up vector and view direction colinear */
if (EqualVertex(basisa, origin))
{
return (false);
}
basisc = CrossProduct(basisb, basisa);
/* Do we have legal camera apertures ? */
if ([Link] < EPSILON || [Link] < EPSILON)
{
return (false);
}
/* Calculate camera aperture statics, note: angles in degrees */
tanthetah = [Link]([Link] * DTOR / 2);
tanthetav = [Link]([Link] * DTOR / 2);
/* Do we have a legal camera zoom ? */
if ([Link] < EPSILON)
{
return (false);
}
/* Are the clipping planes legal ? */
if ([Link] < 0 || [Link] < 0 || [Link] <= [Link])
{
return (false);
}
return true;
}
public void Trans_World2Eye(_3Dpoint w, _3Dpoint e)
{
/* Translate world so that the camera is at the origin */
w.x -= [Link].x;
w.y -= [Link].y;
w.z -= [Link].z;
34
/* Convert to eye coordinates using basis vectors */
e.x = w.x * basisa.x + w.y * basisa.y + w.z * basisa.z;
e.y = w.x * basisb.x + w.y * basisb.y + w.z * basisb.z;
e.z = w.x * basisc.x + w.y * basisc.y + w.z * basisc.z;
}
public bool Trans_ClipEye(_3Dpoint e1, _3Dpoint e2)
{
double mu;
/* Is the vector totally in front of the front cutting plane ? */
if (e1.y <= [Link] && e2.y <= [Link])
{
return (false);
}
/* Is the vector totally behind the back cutting plane ? */
if (e1.y >= [Link] && e2.y >= [Link])
{
return (false);
}
/* Is the vector partly in front of the front cutting plane ? */
if ((e1.y < [Link] && e2.y > [Link]) ||
(e1.y > [Link] && e2.y < [Link]))
{
mu = ([Link] - e1.y) / (e2.y - e1.y);
if (e1.y < [Link])
{
e1.x = e1.x + mu * (e2.x - e1.x);
e1.z = e1.z + mu * (e2.z - e1.z);
e1.y = [Link];
}
else
{
e2.x = e1.x + mu * (e2.x - e1.x);
e2.z = e1.z + mu * (e2.z - e1.z);
e2.y = [Link];
}
}
/* Is the vector partly behind the back cutting plane ? */
if ((e1.y < [Link] && e2.y > [Link]) ||
(e1.y > [Link] && e2.y < [Link]))
{
mu = ([Link] - e1.y) / (e2.y - e1.y);
if (e1.y < [Link])
{
e2.x = e1.x + mu * (e2.x - e1.x);
e2.z = e1.z + mu * (e2.z - e1.z);
e2.y = [Link];
}
35
else
{
e1.x = e1.x + mu * (e2.x - e1.x);
e1.z = e1.z + mu * (e2.z - e1.z);
e1.y = [Link];
}
}
return (true);
}
public void Trans_Eye2Norm(_3Dpoint e, _3Dpoint n)
{
double d;
if ([Link] == 0)
{
d = [Link] / e.y;
n.x = d * e.x / tanthetah;
n.y = e.y;
n.z = d * e.z / tanthetav;
}
else
{
n.x = [Link] * e.x / tanthetah;
n.y = e.y;
n.z = [Link] * e.z / tanthetav;
}
}
public bool Trans_ClipNorm(_3Dpoint n1, _3Dpoint n2)
{
double mu;
/* Is the line segment totally right of x = 1 ? */
if (n1.x >= 1 && n2.x >= 1)
return (false);
/* Is the line segment totally left of x = -1 ? */
if (n1.x <= -1 && n2.x <= -1)
return (false);
/* Does the vector cross x = 1 ? */
if ((n1.x > 1 && n2.x < 1) || (n1.x < 1 && n2.x > 1))
{
mu = (1 - n1.x) / (n2.x - n1.x);
if (n1.x < 1)
{
n2.z = n1.z + mu * (n2.z - n1.z);
n2.x = 1;
}
else
{
36
n1.z = n1.z + mu * (n2.z - n1.z);
n1.x = 1;
}
}
/* Does the vector cross x = -1 ? */
if ((n1.x < -1 && n2.x > -1) || (n1.x > -1 && n2.x < -1))
{
mu = (-1 - n1.x) / (n2.x - n1.x);
if (n1.x > -1)
{
n2.z = n1.z + mu * (n2.z - n1.z);
n2.x = -1;
}
else
{
n1.z = n1.z + mu * (n2.z - n1.z);
n1.x = -1;
}
}
/* Is the line segment totally above z = 1 ? */
if (n1.z >= 1 && n2.z >= 1)
return (false);
/* Is the line segment totally below z = -1 ? */
if (n1.z <= -1 && n2.z <= -1)
return (false);
/* Does the vector cross z = 1 ? */
if ((n1.z > 1 && n2.z < 1) || (n1.z < 1 && n2.z > 1))
{
mu = (1 - n1.z) / (n2.z - n1.z);
if (n1.z < 1)
{
n2.x = n1.x + mu * (n2.x - n1.x);
n2.z = 1;
}
else
{
n1.x = n1.x + mu * (n2.x - n1.x);
n1.z = 1;
}
}
/* Does the vector cross z = -1 ? */
if ((n1.z < -1 && n2.z > -1) || (n1.z > -1 && n2.z < -1))
{
mu = (-1 - n1.z) / (n2.z - n1.z);
if (n1.z > -1)
{
n2.x = n1.x + mu * (n2.x - n1.x);
37
n2.z = -1;
}
else
{
n1.x = n1.x + mu * (n2.x - n1.x);
n1.z = -1;
}
}
return (true);
}
public void Trans_Norm2Screen(_3Dpoint norm, _2Dpoint projected)
{
//[Link]("the value of are");
projected.h = Convert.ToInt32([Link].h - [Link].h * norm.x / 2);
projected.v = Convert.ToInt32([Link].v - [Link].v * norm.z / 2);
}
//public bool Trans_Point();
public bool Trans_Line(_3Dpoint w1, _3Dpoint w2)
{
Trans_World2Eye(w1, e1);
Trans_World2Eye(w2, e2);
if (Trans_ClipEye(e1, e2))
{
Trans_Eye2Norm(e1, n1);
Trans_Eye2Norm(e2, n2);
if (Trans_ClipNorm(n1, n2))
{
Trans_Norm2Screen(n1, p1);
Trans_Norm2Screen(n2, p2);
return (true);
}
}
return (true);
}
public bool Trans_Point(_3Dpoint w1)
{
Trans_World2Eye(w1, e1);
if (e1.y >= [Link] && e1.y <= [Link])
{
Trans_Eye2Norm(e1, n1);
if (n1.x >= -1 && n1.x <= 1 && n1.z >= -1 && n1.z <= 1)
{
Trans_Norm2Screen(n1, p1);
return true;
}
}
return false;
38
}
public void Normalise(_3Dpoint v)
{
double length;
length = [Link](v.x * v.x + v.y * v.y + v.z * v.z);
v.x /= length;
v.y /= length;
v.z /= length;
}
public _3Dpoint CrossProduct(_3Dpoint p1, _3Dpoint p2)
{
_3Dpoint p3;
p3 = new _3Dpoint(0, 0, 0);
p3.x = p1.y * p2.z - p1.z * p2.y;
p3.y = p1.z * p2.x - p1.x * p2.z;
p3.z = p1.x * p2.y - p1.y * p2.x;
return p3;
}
public bool EqualVertex(_3Dpoint p1, _3Dpoint p2)
{
if ([Link](p1.x - p2.x) > EPSILON)
return (false);
if ([Link](p1.y - p2.y) > EPSILON)
return (false);
if ([Link](p1.z - p2.z) > EPSILON)
return (false);
return (true);
}
}
}
[Link]
using System;
using [Link];
using [Link];
using [Link];
namespace Graphics3Dto2D
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
39
static void Main()
{
[Link]();
[Link](false);
[Link](new Form1());
}
}
}
[Link]
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace Graphics3Dto2D
{
class Screen
{
public _2Dpoint center;
public _2Dpoint size;
public Screen()
{
center = new _2Dpoint(720, 420);
size = new _2Dpoint(800, 800);
}
}
}
40
Output:
41