[go: up one dir, main page]

0% found this document useful (0 votes)
48 views44 pages

CAD & PD Lab File

Uploaded by

Shhouraya Chadha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views44 pages

CAD & PD Lab File

Uploaded by

Shhouraya Chadha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

CAD & PRODUCT DESIGN

(MPMEC14)
SEMESTER-05
LAB FILE

BY :
PREETI (2021UMP4377)
SHHOURAYA CHADHA (2021UMP4391)
ANANDITA SINGH (2021UMP7243)
List of Experiments
Total No of Labs: 13
No. of
S. No. Programs List Labs

CAD Experiments

1 Study of basic graphics functions defined in “graphics.h”. 1

2 To implement Digital differential analyzer (DDA) Algorithm for drawing a line 1


segment between two given end points A (x1, y1) and B(x2, y2).

3 To implement Bresenham’s line drawing algorithm for drawing a line segment 1


between two given endpoints A (x1, y2) and B(x2, y2).

4 To implement Bresenham’s Cicrcle drawing algorithm for drawing a circle 1


with a given center of circle P(x, y) and radius r.

5 To apply the basic 2D transformations such as translation, Scaling, Rotation, 2


shearing and reflection for a given 2D object.

6 To apply the basic transformations such as translation, Scaling, Rotation for a 2


given 3D object.

7 To generate a smooth curve by using Bezier curve technique for a given set of 1
control points.

8 To implement Cohen–Sutherland 2D clipping and window–viewport mapping 1

Product Design

9 Introduction to CAD software’s. 1

10 Generation of any one simple solid model using any CAD software. 1

11 Generation of any one assembly of mechanical part using any CAD software. 1
Experiment-01

Aim: Study of basic graphics functions defined in “graphics.h”.

Theory:
What is computer graphics?
• Computer graphics deals with all aspects of creating images with a computer
Hardware
Software
Applications
Computer graphics are graphics created using computers and, more generally, the representation and
manipulation of image data by a computer. The development of computer graphics, or simply referred
to as CG, has made computers easier to interact with, and better for understanding and interpreting
manytypes of data. Developments in computer graphics have had a profound impact on many types of
media and have revolutionized the animation and video game industry. Typically, the term computer
graphics refers to several different things: · the representation and manipulation of image data by a
computer · the various technologies used to create and manipulate images · the images so produced and
The subfield of computer science which studies methods for digitally synthesizing and manipulating
visual content Applications: · Computer Aided Design · Computer simulation · Digital art · Education
· Graphic design · Information visualization ·
Scientific visualization · Video Games · Virtual reality · Web design

BASIC GRAPHICS FUNCTION

1) INITGRAPH
• Initializes the graphics system.
Declaration
• Void far initgraph(int far *graphdriver)
Remarks
To start the graphic system, you must first call initgraph.

• Initgraph initializes the graphic system by loading a graphics driver from disk (or validating a
registered driver) then putting the system into graphics mode.
• Initgraph also resets all graphics settings (color, palette, current position, viewport, etc) to their
defaults then resets graph.

2) GETPIXEL, PUTPIXEL
• Getpixel gets the color of a specified pixel.
• Putpixel places a pixel at a specified point.

Decleration
• Unsigned far getpixel(int x, int y)
• Void far putpixel(int x, int y, int color)
Remarks
• Getpixel gets the color of the pixel located at (x,y);
• Putpixel plots a point in the color defined at (x, y).
Return value
• Getpixel returns the color of the given pixel.
• Putpixel does not return.

3) CLOSE GRAPH
• Shuts down the graphic system.
Decleration
• Void far closegraph(void);
Remarks
• Close graph deallocates all memory allocated by the graphic system.
• It then restores the screen to the mode it was in before you called initgraph.
Return value
None.
4) ARC, CIRCLE, PIESLICE
• arc draws a circular arc.
• Circle draws a circle
• Pieslice draws and fills a circular pieslice
Decleration
• Void far arc(int x, int y, int stangle, int end_angle, int radius);
• Void far circle(int x, int y, int radius);
• Void far pieslice(int x, int y, int stangle, int end_angle, int radius);
Remarks
• Arc draws a circular arc in the current drawing color
• Circle draws a circle in the current drawing color
• Pieslice draws a pieslice in the current drawing color, then fills it using the current fill pattern
and fill color.

5) ELLIPSE, FILLELIPSE, SECTOR


• Ellipse draws an elliptical arc.
• Fillellipse draws and fills ellipse.
• Sector draws and fills an elliptical pie slice.
Decleration
• Void far ellipse(int x, int y, int stangle, int end_angle, int xradius, int yradius)
• Void far fillellipse(int x, int y, int xradius, int yradius)
• Void farsectoe(int x, int y, int stangle, int end_angle, int xradius, int yradius)
Remarks
• Ellipse draws an elliptical arc in the current drawing color.
• Fillellipse draws an elliptical arc in the current drawing color and than fills it with fill color and
fill pattern.
• Sector draws an elliptical pie slice in the current drawing color and than fills it using the pattern
and color defined by setfillstyle or setfillpattern.
6) FLOODFILL
• Flood-fills a bounded region.
Decleration
• Void far floodfill(int x, int y, int border)
Remarks
• Floodfills an enclosed area on bitmap device.
• The area bounded by the color border is flooded with the current fill pattern and fill color.
• (x,y) is a “seed point”
 If the seed is within an enclosed area, the inside will be filled.
 If the seed is outside the enclosed area, the exterior will be filled.
• Use fillpoly instead of floodfill wherever possible so you can maintain code compatibility with
future versions.
• Floodfill doesnot work with the IBM-8514 driver.
Return value

• If an error occurs while flooding a region, graph result returns ‘1’.

7) GETCOLOR, SETCOLOR
• Getcolor returns the current drawing color.
• Setcolor returns the current drawing color.
Decleration
• Int far getcolor(void);
• Void far setcolor(int color)
Remarks
Getcolor returns the current drawing color.
• Setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor.
• To set a drawing color with setcolor , you can pass either the color number or the equivalent
color name.

8) LINE, LINEREL, LINETO


• Line draws a line between two specified pints.
• Onerel draws a line relative distance from current position(CP).
• Linrto draws a line from the current position (CP) to(x,y).
Decleration
• Void far lineto(int x, int y)
Remarks
• Line draws a line from (x1, y1) to (x2, y2) using the current color, line style and thickness.
It does not update the current position (CP).
• Linerel draws a line from the CP to a point that is relative distance (dx, dy) from the CP, then
advances the CP by (dx, dy).
• Lineto draws a line from the CP to (x, y), then moves the CP to (x,y).
Return value
• None

9) RECTANGLE
• Draws a rectangle in graphics mode.
Decleration
• Void far rectangle(int left, int top, int right, int bottom)
Remarks
• It draws a rectangle in the current line style, thickness and drawing color.
• (left, top) is the upper left corner of the rectangle, and (right, bottom) is its lower right corner.

Return value
• None.
Note: Algorithms of some simple programs have also been included.
Here is a sample program that initializes the graphics mode in C Language.
#include<graphics.h> #include<conio.h>
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,""); /* initialization of graphic mode */
circle(150,150,100); getch();
closegraph(); /* Restore orignal screen mode */
} /* End of program */
Experiment - 02
Aim: To implement Digital differential analyzer (DDA) Algorithm for drawing a line segment between
two given end points A (x1, y1) and B(x2, y2).

Description: DDA algorithm is an incremental scan conversion method. Here we perform calculations
at each step using the results from the preceding step. The characteristic of the DDA algorithm is to take
unit steps along one coordinate and compute the corresponding values along the other coordinate. The
unit steps are always along the coordinate of greatest change, e.g. if dx = 10 and dy = 5, then we would
take unit steps along x and compute the steps along y.
In DDA we need to consider two cases;
One is slope of the line less than or equal to one (|m| ≤1) and slope of the line greater than one (|m| >
1).
1) When |m| ≤ 1 means y2-y1 = x2-x1 or y2-y1 <x2-x1.In both these cases we assume x to be the major
axis. Therefore we sample x axis at unit intervals and find the y values corresponding to each x
value. We have the slope equation as
Δ y = m Δ x y2-y1 =
m (x2-x1)
In general terms we can say that y i+1 - yi = m(x i+1 - xi ). But here Δ x = 1; therefore the
equation reduces to y i+1= yi + m = yi + dy/dx.
2) When | m| > 1 means y2-y1> x2-x1 and therefore we assume y to be the major axis. Here we
sample y axis at unit intervals and find the x values corresponding to each y value. We have the
slope equation as
Δ y = m Δ x y2-y1 =
m (x2-x1)

Algorithm

1. Start.
2. Declare variables x,y,x1,y1,x2,y2,k,dx,dy,s,xi,yi and also declare gdriver=DETECT,gmode.
3. Initialise the graphic mode with the path location in TC folder.
4. Input the two line end-points and store the left end-points in (x1,y1).
5. Load (x1,y1) into the frame buffer;that is,plot the first point.put x=x1,y=y1.
6. Calculate dx=x2-x1 and dy=y2-y1.
7. If abs(dx) > abs(dy), do s=abs(dx).
a. Otherwise s= abs(dy).
8. Then xi=dx/s and yi=dy/s.
9. Start from k=0 and continuing till k<s,the points will be
a. x=x+xi.
b. y=y+yi.
10. Place pixels using putpixel at points (x,y) in specified colour.
11. Close Graph.
12. Stop.

Programme:

#include<stdio.h>
#include<graphics.h>
#include<math.h> float
round(float a); void main()
{
int gd=DETECT,gm; // gd=graphics driver (detects best graphics driver and assigns it as default,
gm=graphics mode.) int x1,y1,x2,y2,steps,k; float xincr,yincr,x,y,dx,dy;
printf("enter x1,y1");
scanf("%d%d",&x1,&y1); printf("enter
x2,y2"); scanf("%d%d",&x2,&y2);
initgraph(&gd,&gm,"c:\\turboc3\\BGI");//initializes the graph
dx=x2-x1; dy=y2-y1; if(abs(dx)>abs(dy))
steps=abs(dx); else
steps=abs(dy);
xincr=dx/steps;
yincr=dy/steps;
x=x1; y=y1;
for(k=1;k<=steps;k++)
{ delay(100);//for seeing the line drawing process slowly.
x+=xincr; y+=yincr;
putpixel(round(x),round(y),WHITE);
} outtextxy(200,20,"DDA"); // for printing text at desired screen location.
outtextxy(x1+5,y1-5,"(x1,y1)"); outtextxy(x2+5,y2+5,"(x2,y2)");
getch(); closegraph(); // closes the graph and comes back to previous graphic
mode.
}
float round(float a)
{ int b=a+0.5;
return b;
}
Experiment - 03
Aim: To implement Bresenham’s line drawing algorithm for drawing a line segment between two
given endpoints A (x1, y2) and B(x2, y2).

Description:
The disadvantages associated with DDA algorithm such as real number coordinate calculations and
rounding off operation of real numbers are eliminated in Bresenham’s algorithm. Bresenham developed
an accurate and efficient method of raster generation algorithms to display lines, circles, ellipses and
other curves incorporating only incremental integer calculations.
The scan conversion of a line with positive slope less than unity is considered. Let the starting point of
the line is (xA, yA). Suppose we have calculated the current pixel location for the line as (xk , yk ) , as shown
in Fig. 1. Now, this is decided that which pixel should be selected for display at the sampling column
xk+1. Alternatively, we have two options for the selection of new pixel location, (xk+1, yk ) or (xk+1, yk+1) . Let,

at the sampling column xk+1, the vertical separations between the pixels (xk+1, yk ) and (xk+1, yk+1) , from the
mathematically actual line path be d1 and d2 , respectively (Fig. 2). At the sampling column xk+1 , y

coordinate is given as

y =m.xk+1 +c (1)
Moreover, vertical separations d1 = y− yk = (m.xk+1 +c) − yk (2)
d2 = yk+1 − y = yk+1 − (m.xk+1 +c) (3)
Therefore, d1 −d2 ={(m.xk+1 +c) − yk}−{yk+1 − (m.xk+1 +c)}

= 2.m.xk+1 − yk − yk+1 + 2.c


= 2.m.xk+1 − 2.yk + 2.c−1 (because yk+1 = yk +1) (4)
Actual line
y = m.x + c

Pixel (xk+1, yk+1)


yk+2

yk+1

yk
Pixel (xk+1, yk)
yk-1
x k −1 xk xk+1 xk+2
Pixel No.
Figure 1: Choice of pixel in new location at sampling column xk+1
Actual line
y = m.x + c

d2

yk+1
y
yk d1

xk xk+1

Pixel No.
Figure 2: Vertical separations between pixel positions yk+1 and yk

A decision parameter Pk for the kth step in line drawing algorithm calculated by rearranging the terms in
eqn. (4) so that it incorporates only integer calculations for determining the new pixel locations. Let, x

= xB −xA and y = yB − yA are the horizontal and vertical separations between the endpoint positions A and

B, and slope of the line m= y/ x. Further, a decision parameter Pk , defined as

Pk = x.(d1 −d2) (5)


= x.(2.m.xk+1 − 2.yk + 2.c−1)
= 2. y.(xk +1) − 2. x.yk + 2.c. x− x (because xk+1 = xk +1)
= 2. y.xk − 2. x.yk +{2. y+ x.(2c−1)}
or Pk = 2. y.xk − 2. x.yk +b (6)

where b= 2. y+ x.(2c−1) . For positive slope ( x 0), the sign of decision parameter Pk will be same as the

sign of (d1 −d2), and parameter b is independent of the pixel position. In fact, the parameter b depends

upon the incremental values ( x and y ), which eliminates during the recursive calculations of decision

parameter Pk . It is obvious from Fig. 2 that at new pixel position xk+1, if pixel at yk position is closer to the
actual line path (alternatively, d1 d2 ) than the pixel at yk+1 location, then decision parameter Pk will be
negative (eqn. 5) and lower pixel yk will be selected; otherwise, the upper pixel yk+1 should be plotted. For

recursive calculations, at step k+1, the decision parameter evaluated as


Pk+1= 2. y.xk+1 − 2. x.yk+1 +b (7)

Thus, Pk+1 −Pk = { 2. y.xk+1 − 2. x.yk+1 +b }-{ 2. y.xk − 2. x.yk +b}

= 2. y.(xk +1) − 2. x.yk+1 − 2. y.xk + 2. x.yk (because xk+1 = xk +1)

or Pk+1 = Pk + 2. y− 2. x.(yk+1 − yk )

Depending upon sign of the decision parameter Pk , either yk+1 − yk = 0 or 1. When parameter Pk is negative
(alternatively,d1 d2 ), yk+1 − yk = 0 or yk+1 = yk ; therefore, pixel at yk should be brightened. However, if Pk is
positive (alternatively,d1 d2 ), yk+1 − yk =1 or yk+1 = yk +1; therefore, pixel yk+1 becomes closer to the actual
line path; hence, this should be energized.

The recursive calculations of decision parameter Pk determine each integer x pixel position, starting from
left coordinate endpoint (xA, yA) of the line AB . The initial value of decision parameter P0 may be evaluated
for the starting pixel position (xA, yA), with the assumption that line passes through the origin (alternatively,

xk = yk = 0 and c = 0 , from eqn. 6) as

P0 = 2. y.0−2. x.0+{2. y+ x.(2.0−1)} = 2. y− x (9)

Algorithm:
Line generation algorithm when slope m <1 (i.e., slope is in between 0o and 45o)
Step 1: Input the line endpoints AB as (xA, yA) and (xB, yB ), respectively.

Step 2: Plot the first endpoint (xA, yA), i.e., load the point into the frame buffer.

Step 3: Calculate the initial value of decision parameter Pk , i.e. P0


x = xB −xA ; y = yB − yA and P0 = 2. y− x

Step 4: At each xk position, starting from k =0 , perform the following test:


If Pk 1 (i.e., negative), the next position to plot the pixel is (xk +1, yk ) and Pk+1 = Pk + 2. y
Otherwise ( Pk 1, i.e., positive), the next pixel position is (xk +1, yk +1)

and Pk+1 = Pk + 2. y− 2. x

Step 5: Plot a point of current (x, y) position.

Step 6: Repeat Step 4 through Step 5, x times until reach the second endpoint, i.e. x xB .

Line generation algorithm when slope m >1 (i.e., slope is in between 45o and 90o)
The procedure for line generation algorithm is same except that x and y coordinates are interchanged.
Step 1: Input the line endpoints AB as (xA, yA) and (xB, yB ), respectively.

Step 2: Plot the first endpoint (xA, yA), i.e., load the point into the frame buffer.

Step 3: Calculate the initial value of the decision parameter Pk , i.e. P0


x = xB −xA ; y = yB − yA and P0 = 2. x− y

Step 4: At each xk position, starting from k =0 , perform the following test:


If Pk 1 (i.e., negative), the next position to plot the pixel is (xk , yk +1) and Pk+1 = Pk + 2. x
Otherwise ( Pk 1, i.e., positive), the next pixel position is (xk +1, yk +1)
and Pk+1 = Pk + 2. x− 2. y

Step 5: Plot a point of current (x, y) position.

Step 6: Repeat Step 4 through Step 5, y times until reach the second endpoint, i.e. y yB .
Program:
include<stdio.h>
#include<conio.h>
#include<graphics.h> void
main()
{
int x,y,x1,y1,x2,y2,p,dx,dy; int
gd=DETECT,gm;
initgraph(&gd,&gm,"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=p+2*dy;
1;
p=p+(2*dy)-(2*dx);
} putpixel(x,y,7); }
getch();
closegraph();
Experiment - 04
Aim: To implement Bresenham’s Cicrcle drawing algorithm for drawing a circle with a given center of
circle P(x, y) and radius r.

Description:
Bresenham’s algorithm extensively uses symmetry of the circle. This is fact that if 18 part of the circle is
generated then rest 78 part of the circle can be generated using the symmetry, as shown in Fig. 3.16. If (x,
y) coordinates of a point P1 in the first octant is calculated, it is easy to find out the remaining seven points

in the other seven octants such as P2(y,x), P3(y,−x), P4(x,−y), P5(−x,−y), P6(−y,−x), P7(−y, x) and P8(−x, y).

Therefore, in Bresenham’s midpoint circle generating algorithm, it is required to find out pixel positions
only in the region of 18 part of the circle (i.e., one octant), and hence, development of software is very
easy. Moreover, the circle draws at a faster rate.

VIII Octant I Octant


y = x line
P8 (−x, y ) P1 ( x, y )
• •
P7 (−y, x) P2 ( y, x)
• •
VII Octant II Octant

VI III Octant
Octant
P6 (−y,−x) • • P3 ( y,−x)
• •
P5 (−x,−y ) P4 ( x,−y )
V Octant IV y = - x line
Octant
In this method, at first, the topmost point, i.e., (0,r) is plotted if equation of circle is x2 + y2 = r2 . However,

for circle with centre position (xc, yc) and radius r, the algorithm can be developed for the calculation of
pixel positions around the circle path, centered at origin (0,0) . Thereafter, each evaluated pixel position
(x, y) is shifted to its proper screen position by adding xc to x and yc to y along circle section in the first

octant (from x = 0 line to x= y line; alternatively, when slope varies from m=0 to m=−1). Therefore, we can

go in the positive x -direction by unit steps over the first octant. When x coordinate of a point increases,
corresponding y coordinate decreases.

Similar to other Bresenham’s algorithms, a decision parameter Pk determines the two possible pixel
positions, which is closer to the actual circle boundary at each step in the first octant. Pixel positions in
the other seven octants are determined using the symmetry of circle. Let circle function itself is the
decision parameter in the Bresenham’s algorithm. Thus, the equation of circle in implicit form is
< 0, if point (x, y) lies inside the circle boundary
Pk = fcircle(x, y) = x2 + y2 −r2 = 0, if point (x, y) lies on the circle boundary
> 0, if point (x, y) lies outside the circle boundary

Suppose, Fig. 3.17 shows the plot for the current pixel coordinates (xk , yk ) . Now, it is to decide that at the
sampling position xk+1, whether the pixel yk or one at yk−1 is closer to the circle boundary. Alternatively, at
position xk+1, we have to select either pixel coordinate position (xk+1, yk ) as choice I or pixel position (xk+1,
yk−1) as choice II, depending upon position of the midpoint between two adjacent pixels yk and yk−1 from
the circle boundary. The decision parameter Pk at the midpoint between these two pixels will be

Pk = fcircle(xk+1, yk + yk−1 ) = fcircle(xk +1, yk − 12) (1)


2

= (xk +1)2 + (yk − 12)2 −r2


Choice I
Scan line

(0, r )
yk Mid point

y k −1
Circle
boundar
y k −2 y
Choice II

xk xk +1 xk +2

Pixel No.
Now, there are two possibilities for the decision parameter Pk :
(i). If Pk 0, the midpoint lies inside the circle; alternatively, pixel on the scan line yk is closer to the
circle boundary and pixel choice will be (xk+1, yk ) , i.e., choice I.
(ii). If Pk 0 , the midpoint lies outside or on the circle boundary; alternatively, pixel on the scan line
yk−1becomes closer to the circle boundary and pixel choice will be (xk+1, yk−1) , i.e., choice
II. The recursive formula for successive values of the decision parameter Pk may be developed by using

the incremental calculations for the next pixel position at xk+1 +1 = xk + 2. Thus, recursive expression for

Pk+1 may be written as

Pk+1 = fcircle(xk+1 +1, yk+1 − 12)

= {(xk +1)2 +1}2 +{yk+1 − 12}2 −r2

= (xk +1)2 +1+ 2.(xk +1) + yk2+1 + 14 − yk+1 −r2

= {(xk +1)2 + (yk2 + − yk ) −r2}+1+ 2.(xk +1) + yk2+1 − yk+1 − yk2 + yk


= {(xk +1)2 + (yk2 + − yk ) −r2}+1+ 2.(xk +1) + yk2+1 − yk+1 − yk2 + yk

or Pk+1 = Pk + 2.(xk +1)+(yk2+1 − yk2)−(yk+1 − yk )+1 (2)


where yk+1 = yk (if Pk 0, i.e., negative) when pixel (xk+1, yk ) lies inside the circle boundary;
therefore, Pk+1 = Pk + 2.xk+1 +1 (3)

and yk+1 = yk−1 (if Pk 0 , i.e., positive) when pixel (xk+1, yk−1) lies outside the circle boundary; therefore, yk+1
= yk−1 = yk −1 gives yk+1 − yk =−1 and yk2+1 − yk2 =1−2yk moreover, the decision parameter may be written as
Pk+1 = Pk + 2.xk+1 +1− 2.(yk −1)

= Pk + 2.xk+1 +1− 2.yk+1 (because yk+1 = yk −1) (4)


From eqn. (3.13), the initial value of decision parameter at topmost point (0, r) , calculated as

P0 = fcircle(1, r − ) =1+ (r − 12)2 −r2= 54 − r (5)


When radius of the circle is specified as integer value, P0 may be modified as
P0 1−r

Algorithm
1. Start.
2. Declare variables x,y,p and also declare gdriver=DETECT,gmode.
3. Initialise the graphic mode with the path location in TC folder.
4. Input the radius of the circle r.
5. Load x-0,y=r,initial decision parameter p=1-r.so the first point is (0,r).
6. Repeat Step 7 while (x<y) and increment x-value simultaneously.
7. If (p>0),do p=p+2*(x-y)+1.
a. Otherwise p=p+2*x+1 and y is decremented simultaneously.
8. Then calculate the value of the function circlepoints() with parameters (x,y).
9. Place pixels using putpixel at points (x+300,y+300) in specified colour in circlepoints()
function shifting the origin to 300 on both x-axis and y-axis.
10. Close Graph.
11. Stop.

Programe

# include<stdio.h>
# include<graphics.h>
# include<conio.h>
# include<math.h>

void bresenham_circle(const int h,const int k,const int r)


{
// cleardevice(); int
x=0,y=r,p=(3-(2*r));
cleardevice();
line(320,1,320,480);
line(1,240,640,240);
do
{
putpixel((h+x),(k+y),15);
putpixel((h+y),(k+x),15); putpixel((h+y),(k-x),15);
putpixel((h+x),(k-y),15); putpixel((h-x),(k-y),15);
putpixel((h-y),(k-x),15); putpixel((h-y),(k+x),15);
putpixel((h-x),(k+y),15);
x++;
if(p<0)
p+=((4*x)+6);
else
{
y--;
p+=((4*(x-y))+10);
}
}
while(x<=y);
}

void main(void)
{
int driver=VGA,mode=VGAHI,h,k,r; initgraph(&driver,&mode,"..\\Bgi");
printf("\n ENTER THE VALUE OF [X- COORDINATE] :"); scanf("%d",&h);
printf("\n ENTER THE VALUE OF [Y- COORDINATE] :"); scanf("%d",&k);
printf("\n ENTER THE VALUE OF RADIUS :"); scanf("%d",&r);
bresenham_circle(320+h,240-k,r);
getch(); }
Experiment-05
Aim: To apply the basic 2D transformations such as translation, Scaling, Rotation, shearing and
reflection for a given 2D object.

Theory: In generalized form, the matrix equation incorporating all five types of geometric
transformations may be expressed as

xT  
A B 0 x x
    =   (1)
y =
 T  C D 0 .y [T ].y
 

where [T ] represents a transformation matrix in homogeneous coordinates. Different 2D geometric


transformation matrices in homogeneous coordinates are
1 0 t x 
Translation: [T ] = 0 1 t  (2)
t  y
0 0 1 
Rotation:

Counter clockwise rotation (ccw) in the xy plane,


cos  sin  0
[T ] = sin  cos 0 (3)

r  
 0 0 1

Clockwise rotation (cw) in the xy plane,


 cos sin  0
[T ] = − sin  cos 0 (4)

r  
 0 0 1

Scaling:
Sx 0 0


a) ] y
0 (5)
= 1

0
S
 0 0
Reflection

About the origin, −1 0 0


 
[R] = 0 −1 0
 (8)
 0 0 1
0 1 0

 
About the line y = x , [R] = 1 0 0 (9)
0 0 1
Shear :
1 Shx 0

Along the x-axis, [Sh ] = 0 1 0 (10)


x  
0 0 1
 1 0 0

Along the y-axis, [Sh ] = Sh 1 0 (11)


y  y 
 0 0 1
1 Shx 0

Along the x and y-axes, [Sh] = Sh 1 0 (12)

Scaling and Shear: The effect of elements A, B, C and D in 3x3 transformation matrix (eqn.
4.18) may be separately identified. The terms B and C cause shear along x and y directions (eqn. 4.23c),
respectively. The terms A and D act as scale factors (eqn. 4.21). Thus, the general 3x3 transformation matrix
(eqn. 4.18) producing a combination of shear and scaling, is expressed as
 Sx Shx 0
a) ] = S 0 (13)
Sh

In general, transformations with a determinant identically equal to +1 gives pure rotations. However,
if the determinant of matrix is identically -1 then the transformation produces pure reflection. If two
pure reflection transformations applied successively about the lines passing through origin, it results
into a pure rotation.
Program for translation:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,tx,ty,x3,y3,x4,y4;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter the starting point of line segment:"); scanf("%d
%d",&x1,&y1); printf("Enter the ending point of line
segment:"); scanf("%d %d",&x2,&y2); printf("Enter
translation distances tx,ty:\n"); scanf("%d%d",&tx,&ty);
setcolor(5); line(x1,y1,x2,y2);
outtextxy(x2+2,y2+2,"Original line"); x3=x1+tx; y3=y1+ty;
x4=x2+tx; y4=y2+ty; setcolor(7); line(x3,y3,x4,y4);
outtextxy(x4+2,y4+2,"Line after translation"); getch(); }

Program for scaling:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
float x1,y1,x2,y2,sx,sy,x3,y3,x4,y4;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter the starting point coordinates:");
scanf("%f %f",&x1,&y1);
printf("Enter the ending point coordinates:");
scanf("%f %f",&x2,&y2);
printf("Enter scaling factors sx,sy:\n");
scanf("%f%f",&sx,&sy);
setcolor(5); line(x1,y1,x2,y2);
outtextxy(x2+2,y2+2,"Original line");
x3=x1*sx;
y3=y1*sy;
x4=x2*sx;
y4=y2*sy;
setcolor(7);

line(x3,y3,x4,y4); outtextxy(x3+2,y3+2,"Line
after scaling");getch();
}
Program for Rotation:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
float x1,y1,x2,y2,x3,y3,x4,y4,a,t;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter coordinates of starting point:\n");
scanf("%f%f",&x1,&y1);
printf("Enter coordinates of ending point\n");
scanf("%f%f",&x2,&y2);
printf("Enter angle for rotation\n");
scanf("%f",&a);
setcolor(5);
line(x1,y1,x2,y2);
outtextxy(x2+2,y2+2,"Original line");
t=a*(3.14/180);
x3=(x1*cos(t))-(y1*sin(t));
y3=(x1*sin(t))+(y1*cos(t));
x4=(x2*cos(t))-(y2*sin(t));
y4=(x2*sin(t))+(y2*cos(t));
setcolor(7); line(x3,y3,x4,y4);
outtextxy(x3+2,y3+2,"Line after rotation");
getch();
}
Program for reflection along x-axis:
# include <stdio.h> #
include <conio.h>
# include <graphics.h>#
include <math.h> char
IncFlag;
int PolygonPoints[3][2] ={{10,100},{110,100},{110,200}};
void PolyLine()
{
int iCnt;
cleardevice();
line(0,240,640
,240);
line(320,0,320
,480);
for (iCnt=0; iCnt<3; iCnt++)
{
line(PolygonPoints[iCnt][0],PolygonPoints[iCnt][1],
PolygonPoints[(iCnt+1)%3][0],PolygonPoints[(iCnt+1)%3][1]);
}
}
void Reflect()
{
float
Angl
e;int
iCnt;
int
Tx,Ty;
printf("e
ndl");;
for (iCnt=0; iCnt<3; iCnt++)
PolygonPoints[iCnt][1] = (480 - PolygonPoints[iCnt][1]);
}
void main()
{
int gDriver = DETECT,
gMode; int iCnt;
initgraph(&gDriver, &gMode,
"C:\\TurboC3\\BGI"); for (iCnt=0; iCnt<3;
iCnt++)
{
PolygonPoints[iCnt][0] += 320;
PolygonPoints[iCnt][1] = 240 - PolygonPoints[iCnt][1];
}
Poly
Line
();
getc
h();
Refl
ect()
;
Poly
Line
();
getc
h();
}
Program for Reflection about y-axis:
# include
<stdio.h> #
include
<conio.h>
# include
<graphics.h>#
include
<math.h> char
IncFlag;
int PolygonPoints[3][2] =
{{10,100},{110,100},{110,200}};
void PolyLine()
{
int iCnt;
cleardevice();
line(0,240,640
,240);
line(320,0,320
,480);
for (iCnt=0; iCnt<3; iCnt++)
{
line(PolygonPoints[iCnt][0],PolygonPoints[iCnt][1],
PolygonPoints[(iCnt+1)%3][0],PolygonPoints[(iCnt+1)%3][1]);
}
}
void Reflect()
{
float
Angl
e;int
iCnt;
int Tx,Ty;
for (iCnt=0; iCnt<3; iCnt++)
PolygonPoints[iCnt][0] = (640 - PolygonPoints[iCnt][0]);
}
void main()
{
int gd =
DETECT, gm;int
iCnt;
initgraph(&gd, &gm,
"C:\\TurboC3\\BGI"); for (iCnt=0;
iCnt<3; iCnt++)
{
PolygonPoints[iCnt][0] += 320;
PolygonPoints[iCnt][1] = 240 - PolygonPoints[iCnt][1];
}
Poly
Line
();
getc
h();
Refl
ect()
;
Poly
Line
();
getc
h();
}
3
4
Program for X-shear:
#include<stdio.h>
#include<conio.h>
#include<dos.h>

#include<graphics.h>
void main()
{
int gd=DETECT,gm;
float shx,shy;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter shear factor shx along x-axis :");
scanf("%f",&shx);
line(100,0,200,0);
line(200,0,200,200);
line(200,200,100,200);
line(100,200,100,0);
printf("X-shear");
setcolor(12);
line((100+(0*shx)),0,(200+(0*shx)),0);
line((200+(0*shx)),0,(200+(200*shx)),200);
line((200+(200*shx)),200,(100+(200*shx)),200);
line((100+(200*shx)),200,(100+(0*shx)),0);
getch();
}
Program for Y-shear:
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h> void
main()
{
int gd=DETECT,gm; float
shx,shy;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter shear factor shy along y-axis :");
scanf("%f",&shy);
line(100,10,200,10);
line(200,10,200,200);
line(200,200,100,200);
line(100,200,100,10);
printf("Y-shear");
setcolor(12);
line(100,10+(shy*100),200,10+(shy*200));
line(200,10+(shy*200),200,200+(shy*200));
line(200,200+(shy*200),100,200+(shy*100));
line(100,200+(shy*100),100,10+(shy*100));
Experiment-06
Aim: To apply the basic transformations such as translation, Scaling, Rotation for a given 3D
object.
Theory:
Translation
In three-dimensional homogeneous coordinates, a point translates from P(x, y, z,1) to

PT (xT , yT , zT ,1) using the matrix equation


xT  x  1 0 0 tx   x 
y   y 0 1 0 t   y
 T   
= [T ]. = y  
  t  

. 

 zT   z  0 0 1 tz   z 
 1  1   1 1
0 0 0
 
Rotation
Three-dimensional z -axis rotation equation in matrix form is expressed as
cos 0 0 s
sin  

[Trz ] = 0 0 i

n

c
o
s

 0 0 1 0
 
 0 0 0 1

Three-dimensional x -axis rotation equation in matrix form is expressed as


1 0 0 0
0 − sin  0
cos  
[Trx ] =  cos  0
0 sin 
 
0 0 0 1

Three-dimensional y -axis rotation equation in matrix form is expressed as


 cos 0 sin 0
 0 1 0 
[Try ] =  0
− sin  0 cos 0
0 0 0 
Scaling 1
xT 
x Sx 0
0 0 x 
y    0 0  y
 T y S 0
= [S ].  =  y  
     . 

Program for translation


#include #include
#include<math.h>
#include<process.h>
#include<graphics.h>
int x1,x2,y1,y2,mx,my,depth;
void draw();
void trans();
void main()
{
int gd=DETECT,gm,c;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\n\t\t3D Translation\n\n");
printf("\nEnter 1st top value(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter right bottom value(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-x1)/4;
mx=(x1+x2)/2;
my=(y1+y2)/2;
draw();
getch();
cleardevice();
trans();
getch();
}
void draw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
void trans()
{
int a1,a2,b1,b2,dep,x,y;
printf("\n Enter the Translation Distances:");
scanf("%d%d",&x,&y);
a1=x1+x;
a2=x2+x;
b1=y1+y;
b2=y2+y;
dep=(a2-a1)/4;
bar3d(a1,b1,a2,b2,dep,1);
setcolor(5);
draw();
}
Program for Rotation:

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int x1,x2,y1,y2,mx,my,depth;
void draw();
void rotate();
void main()
{
int gd=DETECT,gm,c;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\n3D Transformation Rotating\n\n");
printf("\nEnter 1st top value(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter right bottom value(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-x1)/4;
mx=(x1+x2)/2;
my=(y1+y2)/2;
draw(); getch();
cleardevice();
rotate();
getch();
}
void draw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
void rotate()
{
float t;
int a1,b1,a2,b2,dep;
printf("Enter the angle to rotate=");
scanf("%f",&t);
t=t*(3.14/180);
a1=mx+(x1-mx)*cos(t)-(y1-my)*sin(t);
a2=mx+(x2-mx)*cos(t)-(y2-my)*sin(t);
b1=my+(x1-mx)*sin(t)-(y1-my)*cos(t);
b2=my+(x2-mx)*sin(t)-(y2-my)*cos(t);
if(a2>a1)
dep=(a2-a1)/4;
else
dep=(a1-a2)/4;
bar3d(a1,b1,a2,b2,dep,1); setcolor(5);

Program for scaling:


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<graphics.h>
int x1,x2,y1,y2,mx,my,depth;
void draw();
void scale();
void main()
{
int gd=DETECT,gm,c;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\n\t\t3D Scaling\n\n");
printf("\nEnter 1st top value(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter right bottom value(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-x1)/4;
mx=(x1+x2)/2;
my=(y1+y2)/2;
draw();
getch();
cleardevice();
scale();
getch();
}
void draw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
void scale()
{
int x,y,a1,a2,b1,b2,dep;
printf("\n\n Enter scaling Factors:");
scanf("%d%d",&x,&y);
a1=mx+(x1-mx)*x;
a2=mx+(x2-mx)*x;
b1=my+(y1-my)*y;
b2=my+(y2-my)*y;
dep=(a2-a1)/4;
bar3d(a1,b1,a2,b2,dep,1);
setcolor(5);
draw();
}
Experiment - 07
Aim: To generate a smooth curve by using Bezier curve technique for a given set of control points.

Description:
A Bézier curve is a parametric curve frequently used in computer graphics and related fields.
Generalizations of Bézier curves to higher dimensions are called Bézier surfaces, of which the
Béziertriangle is a special case.
In vector graphics, Bézier curves are used to model smooth curves that can be scaled indefinitely.
"Paths," as they are commonly referred to in image manipulation programs are combinations of linked
Bézier curves. Paths are not bound by the limits of rasterized images and are intuitive to modify.
Bézier curves are also used in animation as a tool to control motion
Four points P0, P1, P2 and P3 in the plane or in higher-dimensional space define a cubic Bézier curve.
The curve starts at P0 going toward P1 and arrives at P3 coming from the direction of P2. Usually, it
will not pass through P1 or P2; these points are only there to provide directional information. The
distance between P0 and P1 determines "how long" the curve moves into direction P2 before turning
towards P3.

Program for Bezier curve:


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

return;
}
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);
}
Experiment – 08
Aim: To implement Cohen–Sutherland 2D clipping and window–viewport mapping

Algorithm:
Step 1: Start the program.
Step 2: Enter the line end points and the window coordinates.
Step 3: Every line end point is assigned a code that identified the location of the point relative to the
boundaries of the clipping rectangle.
Step 4: Check whether the line lies inside the window then it is entirely drawn.
Step 5: Check whether the line lies outside the window then it is entirely clipped.
Step 6: Otherwise check whether the line intersects the window:
a) Calculate differences between end points and clip boundaries.
b) Determine the intersection point and how much of the line is to be discarded.
Step 7: Display the Output.
Step 8: Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>
typedef struct coord
{
int x,y;
char code[4];
}PT;
void drawwindow();
void drawline(PT p1,PT p2,int c1); PT setcode(PT p);
int visibility(PT p1,PT p2); PT resetendpt(PT p1,PT p2); void main()
{
int gd=DETECT,gm,v;
PT p1,p2,ptemp;
initgraph(&gd,&gm," "); cleardevice();
printf("\n\nCOHEN SUDHERLAND AND LINE CLIPPING ALGORITHM\n\n\n");
printf("\n\nENTER THE TWO END POINTS P1(X,Y):\n\n");
scanf("%d%d",&p1.x,&p1.y);
printf("\n\nENTER THE TWO END POINTSp2(X,Y):\n\n"); scanf("%d%d",&p2.x,&p2.y);
cleardevice(); printf("\n\nCLIPPING WINDOW"); drawwindow();
getch();
printf("\n\nCOHEN SUTHERLAND LINE CLIPPING ALGORITHM\n\n");
printf("\n\t\t BEFORE CLIPPING"); drawline(p1,p2,4);
getch(); p1=setcode(p1); p2=setcode(p2); v=visibility(p1,p2); switch(v)
{
case 0:
cleardevice(); drawwindow(); drawline(p1,p2,15); break;
case 1:
cleardevice(); drawwindow(); break;
case 2:
cleardevice(); p1=resetendpt(p1,p2); p2=resetendpt(p2,p1); drawwindow();
printf("\nCOHEN SUTHERLAND CLIPPING ALGORITHM");
printf("\n \t\t AFTER CLIPPING\n\n"); drawline(p1,p2,15);
break;
}
getch(); closegraph();
}
void drawwindow()
{
setcolor(RED);
setlinestyle(DOTTED_LINE,1,1);
line(150,100,100,100);
line(150,100,150,50);
line(450,100,500,100);
line(450,100,450,50);
line(450,350,500,350);
line(450,350,450,400);
line(150,350,150,400);
line(150,350,100,350);
line(150,100,100,100);
line(150,100,150,50);
setlinestyle(SOLID_LINE,1,1);
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
outtextxy(450,30,"1001");
outtextxy(470,200,"1000");
outtextxy(470,370,"1010");
outtextxy(300,370,"0010");
outtextxy(120,370,"0110");
outtextxy(120,200,"0100");
outtextxy(120,30,"0101");
outtextxy(300,30,"0001");
outtextxy(300,200,"0000");
}
void drawline(PT p1,PT p2,int c1)
{
setcolor(c1);
line(p1.x,p1.y,p2.x,p2.y);
}
PT setcode(PT p)
{
PT ptemp;
if(p.y<100) ptemp.code[0]='1';
else ptemp.code[0]='0';
if(p.y>350) ptemp.code[1]='1';
else ptemp.code[1]='0';
if(p.x>200) ptemp.code[2]='1';
else ptemp.code[2]='0';
if(p.x<150) ptemp.code[3]='1';
else ptemp.code[3]='0';
ptemp.x=p.x; ptemp.y=p.y; return(ptemp);
}
int visibility(PT p1,PT p2)
{
int i,flag=0; for(i=0;i<4;i++)
{
if((p1.code[i]!='0')||(p2.code[i]!='0')) flag=1;
}
if(flag==0) return 0; for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i])&&(p1.code[i]=='1')) flag=0;
}
if(flag==0) return(1); return(2);
}
PT resetendpt(PT p1,PT p2)
{
PT temp; int x,y,i; float m,k;
if(p1.code[3]=='1') x=150;
if(p1.code[2]=='1') x=450;
if((p1.code[3]=='1')||(p1.code[2]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x))); temp.y=k;
temp.x=x; for(i=0;i<4;i++)
{
temp.code[i]=p1.code[i]; if(temp.y<=350&&temp.y>=100)

return(temp);
}
if(p1.code[0]=='1') y=100;
if(p1.code[1]=='1') y=350;
if((p1.code[0]=='1')||(p1.code[1]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)(p1.x+(float)(y-p1.y)/m); temp.y=y;
temp.x=k; for(i=0;i<4;i++) temp.code[i]=p1.code[i]; return(temp);
}
}
else return(p1); return(p2);
}
Experiment - 09
Aim - Introduction to CAD Software’s

Theory -

1. Fundamentals of CAD.

2. Traditional Design Process.

3. Computer Aided Design Process.

4. Study of CAD Software’s.

Conclusion -
Experiment – 10
Aim - Generation of any one simple solid model using any CAD Software

Theory –

1) Geometric Modeling.

a) Wire frame model.

b) Surface model.

c) Solid model.

2) Generation of one simple solid model.

- Steps involved in model making procedure.

Conclusion
Experment-11
Aim - Generation of any one assembly of mechanical part using any CAD software

Theory –

1) Generation of one simple mechanical assembly using any CAD software

- Steps involved in model making procedure.

Conclusion

You might also like