0% found this document useful (0 votes)
31 views9 pages

Graphics Lab Report - 2

This document contains source code and explanations for drawing an ellipse, translating and scaling a triangle, and animating a moving car in C graphics. The ellipse drawing algorithm uses a midpoint method to plot points across four quadrants. Translation moves all points of an object by a constant amount, while scaling resizes an object by multiplying coordinates. The car animation draws the car, erases it, and redraws it further along to simulate movement.
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)
31 views9 pages

Graphics Lab Report - 2

This document contains source code and explanations for drawing an ellipse, translating and scaling a triangle, and animating a moving car in C graphics. The ellipse drawing algorithm uses a midpoint method to plot points across four quadrants. Translation moves all points of an object by a constant amount, while scaling resizes an object by multiplying coordinates. The car animation draws the car, erases it, and redraws it further along to simulate movement.
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/ 9

Tribhuvan University

Institute of Engineering
Thapathali Campus, Thapathali

Subject: Computer Graphics


LAB #2
Implementation of Ellipse Drawing Algorithm,
Translation & Scaling

Submitted by:
Name: Atul Shreewastav
Roll No.: THA077BCT013

Submitted to:
Department of Electronics and Computer Engineering
Date: 19th July, 2023
Mid-point Ellipse Drawing Algorithm
Title: To use draw and reflect a triangle about x and y axes.
Theory:
Midpoint ellipse algorithm plots(finds) points of an ellipse on the first quadrant by dividing the
quadrant into two regions.
Each point(x, y) is then projected into the other three quadrants (-x, y), (x, -y), (-x, -y) i.e. it uses
4-way symmetry.

Decision parameter:

Initially, we have two decision parameters p10 in region 1 and p20 in region 2.

These parameters are defined as : p10 in region 1 is given as :

p10=ry2+1/4rx2-rx2ry

Mid-Point Ellipse Algorithm :

• Take the input radius along the x axis and y axis and obtain the center of the ellipse.
• Initially, we assume the ellipse to be centered at the origin and the first point as : (x, y0)=
(0, ry).
• Obtain the initial decision parameter for region 1 as: p10=ry2+1/4rx2-rx 2ry
• For every xk position in region 1 :

If p1k<0 then the next point along the is (xk+1 , yk) and p1k+1=p1k+2ry2xk+1+ry2

Else, the next point is (xk+1, yk-1 )

And p1k+1=p1k+2ry2xk+1 – 2rx2yk+1+ry2

• Obtain the initial value in region 2 using the last point (x0, y0) of region 1 as:
p20=ry2(x0+1/2)2+rx2 (y0-1)2-rx2ry2
• At each yk in region 2 starting at k =0 perform the following task.

If p2k>0 the next point is (xk, yk-1) and p2k+1=p2k-2rx2yk+1+rx2

• Else, the next point is (xk+1, yk -1) and p2k+1=p2k+2ry2xk+1 -2rx2yk+1+rx2


• Now obtain the symmetric points in the three quadrants and plot the coordinate value as:
x=x+xc, y=y+yc
• Repeat the steps for region 1 until 2ry2x>=2rx2y
Source Code:

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

void _ellipse(int xc, int yc, int rx, int ry){


int x = 0;
int y = ry;
int p = (ry*ry) - (rx*rx*y) + (rx*rx)/4;
while (2*ry*ry*x < 2*rx*rx*y){
putpixel(xc+x, yc+y, BLACK);
putpixel(xc-x, yc+y, BLACK);
putpixel(xc-x, yc-y, BLACK);
putpixel(xc+x, yc-y, BLACK);
x = x+1;
if (p<0){
p = p + (2*ry*ry*x) + ry*ry;
}
else{
y = y-1;
p = p + (2*ry*ry*x) - (2*rx*rx*y) + rx*rx;
}
delay(100);
}
p = (int)pow(((float)x+0.5)*ry,2) + pow(rx*(y-1),2) - pow(rx*ry,2);
while (y>=0){
y = y-1;
putpixel(xc+x, yc+y, BLACK);
putpixel(xc-x, yc+y, BLACK);
putpixel(xc-x, yc-y, BLACK);
putpixel(xc+x, yc-y, BLACK);
if (p>0){
x++;
p -= (2*rx*rx*y) + rx*rx;
}
else{
p += (2*ry*ry*x) - (2*rx*rx*y) + rx*rx;
}
delay(100);
}
}
int main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
setbkcolor(WHITE);
cleardevice();
_ellipse(300,200,100,50);
delay(500000);
closegraph();
return 0;
}
Output:

Figure 1: Drawing an Ellipse using Mid-point Ellipse drawing algorithm

Conclusion:
Thus, as shown in the program above, we can draw an ellipse using the various functions
available in the graphics.h header file.
Translation and Scaling
Title: To use translate and scale a given triangle.
Theory:
Translation:
A translation process moves every point a constant distance in a specified direction. It can be
described as a rigid motion. A translation can also be interpreted as the addition of a constant vector
to every point, or as shifting the origin of the coordinate system.
Whenever we perform translation of any object we simply translate its each and every point. Some
of basic objects along with their translation can be drawn as:
1. Point Translation P(X, Y) : Here we only translate the x and y coordinates of given point
as per given translation factor dx and dy respectively.
2. Line Translation: The idea to translate a line is to translate both of the end points of the
line by the given translation factor(dx, dy) and then draw a new line with inbuilt graphics
function.
3. Rectangle Translation : Here we translate the x and y coordinates of both given points
A(top left ) and B(bottom right) as per given translation factor dx and dy respectively and
then draw a rectangle with inbuilt graphics function
Scaling:
A scaling transformation alters size of an object. In the scaling process, we either compress or
expand the dimension of the object.
Scaling operation can be achieved by multiplying each vertex coordinate (x, y) of the polygon by
scaling factor sx and sy to produce the transformed coordinates as (x’, y’).

So, x’ = x * sx and y’ = y * sy.


The scaling factor sx, sy scales the object in X and Y direction respectively.
So, the above equation can be represented in matrix form:

Or P’ = S . P
If the scaling factor S is less than 1, then we reduce the size of the object. If the scaling factor S is
greater than 1, then we increase size of the objec
Source Code (Translation & Scaling of a Triangle):
#include<stdio.h>
#include<math.h>
#include<graphics.h>

int main(){
// Initializing graphics variables
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
setbkcolor(WHITE);
cleardevice();
// Initializing drawing variables
int x1=100, x2=150, x3=200, y1=50, y2=200, y3=20;

// Drawing the initial triangle


setcolor(BLACK);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
delay(1000);

// Translating and displaying the triangle


x1 += 50;
x2 += 50;
x3 += 50;

line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
delay(1000);

// Initializing the scaling factors


int sx=2;
int sy=2;

// Scaling and displaying the translated triangle


x1 *= sx; y1 *= sy;
x2 *= sx; y2 *= sy;
x3 *= sx; y3 *= sy;

line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);

// Concluding the program


delay(500000);
closegraph();
return 0;
}
Output:

Figure 2: Scaling & Translation of a Triangle

Conclusion:
Thus, as shown in the program above, we can draw, translate and scale a triangle using the
various functions available in the graphics.h header file.
Source Code (Moving Car):
#include<graphics.h>
#include<stdio.h>

// Function to draw moving car


void draw_moving_car(void){
int i, Ij=0, gd = DETECT, gm;

// Passed three arguments to initgraph


// function to initialize graphics mode
initgraph(&gd, &gm, "");
setbkcolor(WHITE);
cleardevice();

for (i=0; i<=420; i+=10){


// Set color of car as Black
setcolor(BLACK);

// These lines for bonnet and


// body of car
line(0+i, 300, 210+i, 300);
line(50+i, 300, 75+i, 270);
line(75+i, 270, 150+i, 270);
line(150+i, 270, 165+i, 300);
line(0+i, 300, 0+i, 330);
line(210+i, 300, 210+i, 330);

// For left wheel of car


circle(65+i, 330, 15);
circle(65+i, 330, 2);

// For right wheel of car


circle(145+i, 330, 15);
circle(145+i, 330, 2);

// Line left of left wheel


line(0+i, 330, 50+i, 330);

// Line middle of both wheel


line(80+i, 330, 130+i, 330);

// Line right of right wheel


line(210+i, 330, 160+i, 330);

delay(100);

// To erase previous car: draw the


// whole car at same position
// but color using black
setcolor(WHITE);

// Lines for bonnet and body of car


line(0+i, 300, 210+i, 300);
line(50+i, 300, 75+i, 270);
line(75+i, 270, 150+i, 270);
line(150+i, 270, 165+i, 300);
line(0+i, 300, 0+i, 330);
line(210+i, 300, 210+i, 330);

// For left wheel of car


circle(65+i, 330, 15);
circle(65+i, 330, 2);

// For right wheel of car


circle(145+i, 330, 15);
circle(145+i, 330, 2);

// Line left of left wheel


line(0+i, 330, 50+i, 330);

// Line middle of both wheel


line(80+i, 330, 130+i, 330);

// Line right of right wheel


line(210+i, 330, 160+i, 330);
}
getch();
closegraph();
}

int main(){
draw_moving_car();
return 0;
}

Output:

Figure 3: Drawing a Moving Car

Conclusion:
Thus, as shown in the program above, we can draw, translate a car and make it appear as it is
moving by erasing the previous car using the various functions available in the graphics.h header
file.

You might also like