0% found this document useful (0 votes)
4 views31 pages

CG 2

Uploaded by

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

CG 2

Uploaded by

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

Name: s.

tarun
Reg no.:vu22csen0500091

Assignment-01
Digital Differential Analyzer Algorithm:
Algorithm:

Step1: Start Algorithm

Step2: Declare x1,y1,x2,y2,dx,dy,x,y as integer variables.


Step3: Enter
values of
x1,y1,x2,y2. Step4:
Calculate dx = x2-
x1 Step5:
Calculate dy = y2-
y1 Step6: If ABS
(dx) > ABS (dy)
Then step
= abs (dx)
Else

Step7: xinc=dx/step
yinc=d
y/
step
assign
x = x1
assign
y = y1
Step8: Set pixel (x, y)

Step9: x
= x + xinc
y=y+
yinc
Set pixels (Round

(x), Round (y))

Step10: Repeat step

9 until x = x2 Step11:
End Algorithm

Source Code:
Digital Differential Analyzer (DDA) Line Drawing Algorithm In
Python.
Bresenham's Line Drawing Algorithm:
Algorithm:
Step1: Start Algorithm

Step2: Declare variable x1,x2,y1,y2,d,i1,i2,dx,dy


Step3: Enter value of x1,y1,x2,y2
Where x1,y1are coordinates of
starting point And x2,y2 are
coordinates of Ending point

Step4: Calculate dx = x2-x1


Calculate dy =
y2-y1 Calculate
i1=2*dy
Calculate
i2=2*(dy-dx)
Calculate d=i1-
dx

Step5: Consider (x, y) as starting point and xend as maximum possible value of
x.
If dx < 0
Then x
= x2 y =
y2
xend=x
1
If dx > 0
Then x =
x 1 y = y1
xend=x2

Step6: Generate points at (x,y)coordinates.

Step7: Check if the whole line is generated.


If x > =
xend
Stop.

Step8: Calculate coordinates of


the next pixel If d < 0
Then d = d
+ i1 If d ≥ 0
Then d = d + i2
Increment y =
y+1

Step9: Increment x = x + 1

Step10: Draw a point of latest (x, y) coordinates

Step11: Go to step 7

Step12: End of Algorithm


Circle Midpoint Algorithm:
Algorithm:

Step 1: Get the input of the coordinates for the centre of the circle and radius of the
circle and store them as x, y and R respectively. Initialize two new variables p and q and
set them as P = 0 and Q = R.

Step 2: Input the initial value of the decision parameter as D = 5/4 – R.

Step 3: Repeat through step 8, while x<y.

Step 4: Call the function of drawing the circle (X, Y, P and Q).

Step 5: Increment the value of P.

Step 6: If D<0 then D = D + 4P + 6

Step 7: Else set Y = Y – 1 and D = D+ 4(P-Q) + 10.

Step 8: Call draw circle (X, Y, P and Q).


Ellipse Midpoint Algorithm:
Algorithm:

Take the input and ellipse centre and obtain


the first point on an ellipse centered on the
origin as a (x,y_{0})=(0,r_{y}) .

2. Now calculate the initial decision


parameter in region 1 as:
p1_{0}={r_{v}}^{2}+1/4{r_{x}}^{2}-
{r_{x}}^{2}r_{v}

3. At each Xx position in region 1 perform the


following task. If p1_{k}<0 then the next
point along the ellipse centered on (0,0) is
(x_{k}+1,y_{k}) i.e.p1_{k+1}=p1_{k}+
{}^{2}{r_{v}}^{2}x_{k+1}+{r_{v}}^{2}

Otherwise the next point along the circle is


(x_{k+1},y_{k}-1)

i.e. p1k+1=p1k+2ry²xk+1-2rx²yk+1+ry2

4. Now, again calculate the initial value in


region 2 using the last point (xo,yo)
calculated in a region 1 as:

p2_{0}={r_{v}}^{2}(x_{0}+1/2)^{2}+
{r_{x}}^{2}(y_{0}-1)^{2}-{r_{x}}^{2}
{r_{y}}^{2}

5. At each yk position in region 2 starting at


k=0 perform the following task. If p2_{k}<0
the next point along the ellipse centered on
(0,0) is (Xk, Ук-1)

i.e. p2k+1=p2k-2rx²yk+1+rx²

Otherwise the next point along the circle will


be (x_{k+1},y_{k}-1)

i.e. p2k+1 =p2k+2ry2Xk+1-2rx²yk+1+x2

6. Now determine the symmetric points in


another three quadrants.

7. Plot the coordinate value as: x=x+Xc,


y=y+yc

8. Repeat the steps for region 1 until


2{r_{y}}^{2}x>=2{r_{x}}^{2}y.
Polygon Drawing Algorithm:
Algorithm(4-Connected Polygon)

Step 1 − Initialize the value of seed point seedx,seedy, fcolor and dcol.

Step 2 − Define the boundary values of the polygon.

Step 3 − Check if the current seed point is of default color, then repeat the
steps 4 and 5 till the boundary pixels are reached.

If getpixel(x, y) = dcol then repeat step 4 and 5

Step 4 − Change the default color with the fill color at

the seed point. setPixel(seedx, seedy, fcol)

Step 5 − Recursively follow the procedure with four neighborhood points.


FloodFill (seedx – 1, seedy,
fcol, dcol) FloodFill (seedx + 1,
seedy, fcol, dcol) FloodFill
(seedx, seedy - 1, fcol, dcol)
FloodFill (seedx – 1, seedy +
1, fcol, dcol)

Step 6 − Exit
Text Drawing Algorithm:
Algorithm:

Step 1: Initialize
Set the current position to the starting
position (seedx, seedy). Set the fill color
(fcol) and default color (dcol).
Iterate through each character in the text.

Step 2: Character Rendering Loop


For each character in the text:
Determine the bitmap or shape representing the character. This can be
obtained from a font file or a predefined set of character bitmaps.
Proceed to Step 3.

Step 3: Flood Fill Algorithm


Implement the Flood Fill algorithm to fill the interior of the
character shape with the fill color: Check if the current seed
point is of the default color.
If getPixel(seedx, seedy) = dcol, then:
Change the default color with the fill color at the seed point:
setPixel(seedx, seedy, fcol). Recursively follow the procedure
with four neighborhood points:
FloodFill(seedx - 1, seedy, fcol, dcol)
FloodFill(seedx + 1,
seedy, fcol, dcol)
FloodFill(seedx, seedy -
1, fcol, dcol)
FloodFill(seedx, seedy +
1, fcol, dcol)

Step 4: Update Position


Move the current position to the right by the width of the character (plus
any desired spacing).

Step 5: Continue Loop


Repeat the Character Rendering Loop for the next character in the text.

Step 6: Display Result


The display buffer now contains the rendered text.

You might also like