0% found this document useful (0 votes)
19 views53 pages

Lecture 6-Line Drawing and 2D Transformations

The Midpoint Circle Drawing Algorithm calculates pixel positions for drawing a circle based on a specified radius and center point. It utilizes decision parameters to determine the closest pixel positions along the circle's path, effectively plotting points in all octants. The document also includes examples and code snippets for implementing the algorithm in a graphical application.

Uploaded by

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

Lecture 6-Line Drawing and 2D Transformations

The Midpoint Circle Drawing Algorithm calculates pixel positions for drawing a circle based on a specified radius and center point. It utilizes decision parameters to determine the closest pixel positions along the circle's path, effectively plotting points in all octants. The document also includes examples and code snippets for implementing the algorithm in a graphical application.

Uploaded by

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

Midpoint Circle Drawing Algorithm

• To determine the closest pixel position to the


specified circle path at each step.
• For given radius r and screen center position (x c,
yc), calculate pixel positions around a circle path
centered at the coodinate origin (0,0).
• Then, move each calculated position (x, y) to its
proper screen position by adding xc to x and yc to y.
• Along the circle section from x=0 to x=y in the first
quadrant, the gradient varies from 0 to -1.

1
Midpoint Circle Drawing Algorithm
 8 segments of octants for a circle:

2
Midpoint Circle Drawing Algorithm
 Circle function: fcircle (x,y) = x2 + y2 –r2

{
> 0, (x,y) outside the circle

fcircle (x,y) = < 0, (x,y) inside the circle

= 0, (x,y) is on the circle


boundary

3
Midpoint Circle Drawing Algorithm
yk yk
midpoint midpoint

yk-1 yk-1

Fk < 0 Fk >= 0
yk+1 = yk yk+1 = yk - 1
Next pixel = (xk+1, yk) Next pixel = (xk+1, yk-1)
Fk+1 = Fk + 2xk+1+1 Fk+1 = Fk + 2xk+1+1 – 2yk+1
4
Midpoint Circle Drawing Algorithm
For the initial point, (x0 , y0) = (0, r)

f0 = fcircle (1, r-½ )


= 1 + (r-½ )2 – r2
= 5–r
4
≈ 1–r

5
Midpoint Circle Drawing Algorithm
void circleMidpoint (int xCenter, int yCenter, int radius)
{ int x = 0;
Int y = radius;
int f = 1 – radius;

circlePlotPoints(xCenter, yCenter, x, y);


while (x < y) {
x++; // increment x value by 1
if (f < 0) // check decision parameter value
f += 2*x+1; // no change in y value
else {
y--; // both x and y values change
f += 2*(x-y)+1;
}
}
circlePlotPoints(xCenter, yCenter, x, y);
}
6
Midpoint Circle Drawing Algorithm
void circlePlotPoints (int xCenter, int yCenter, int x, int y)
{
setPixel (xCenter + x, yCenter + y);
setPixel (xCenter – x, yCenter + y);
setPixel (xCenter + x, yCenter – y);
setPixel (xCenter – x, yCenter – y);
setPixel (xCenter + y, yCenter + x);
setPixel (xCenter – y, yCenter + x);
setPixel (xCenter + y, yCenter – x);
setPixel (xCenter – y, yCenter – x);
}

7
Midpoint Circle Drawing Algorithm
Example 1:
Given a circle radius = 10, determine the circle octant in the
first quadrant from x=0 to x=y.

Solution:
f0 = 5 – r
4
= 5 – 10
4
= -8.75
≈ –9
8
Midpoint Circle Drawing Algorithm
Initial (x0, y0) = (0,10)
Decision parameters are: 2x0 = 0, 2y0 = 20
k Fk xk+1 yk+1 2xk+1 2yk+1
0 -9 1 10 2 20
1 -9+2+1=-6 2 10 4 20
2 -6+4+1=-1 3 10 6 20
3 -1+6+1=6 4 9 8 18
4 6+8+1-18=-3 5 9 10 18
5 -3+10+1=8 6 8 12 16
6 8+12+1-16=5 7 7 14 14

9
Example 2:
Given a circle radius r = 15, demonstrate the midpoint circle
algorithm by determining positions along the circle octant in the
first quadrant from x = 0 to x = y.

Solution:

p0 = 1 – r = – 14
Plot the initial point (x0 , y0) = (0, 15),
2x0 = 0 and 2y0 = 30.
Successive decision parameter values and positions
along the circle path are calculated using the midpoint
method as:

10
11
12
Circle and Line using mid point algorithms
// GL_Circle.cpp : Defines the entry point for the console
//application.
#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<GL/glut.h>

void setPixel(GLint xCoord,Glint Coord)//Defining setPixel function


{
glBegin(GL_POINTS);
glVertex2i(xCoord,yCoord);
glEnd();
}

13
//Defining Cirle_Line function
void Circle_Line(void){

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0,0.0,1.0);

int x,y,xs=0,ys=0,xe=800,ye=600,d,R,dx,dy;

//Bresenham's Line Drawing algorithm


dx=xe-xs;
dy=ye-ys;
d=2*dy-dx;
x=xs;y=ys;

14
for (int i=1;i<=dx;i++)
{
setPixel(x,y);
if (d>=0)
{
y=y+1;
d=d-2*dx;
}
else
{
x=x+1;
d=d+2*dy;
}
}

15
//Mid point Circle Drawing Algorithm
x=0;R=150;y=R;
d=3-2*R;
while(x<y)
{
setPixel(x,y);
setPixel(y,x);
setPixel(-x,y);
setPixel(-y,x);
setPixel(-y,-x);
setPixel(-x,-y);
setPixel(x,-y);
setPixel(y,-x);

16
if (d<0) d=d+4*x+6;
else
{
d=d+4*(x-y)+10;
y=y-1;
}
x=x+1;
}

glFlush();
}

17
//main function
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(120,80);
glutInitWindowSize(800,600);
glutCreateWindow("A Circle And A Line");
glClearColor(1.0,1.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-200.0,800.0,-200.0,600.0);
glutDisplayFunc(Circle_Line);
glutMainLoop();
return 0;
}

18
2D Transformations
• What is transformations?
– The geometrical changes of an object from a
current state to modified state.
• Why the transformations is needed?
– To manipulate the initially created object and to
display the modified object without having to
redraw it.
2D Transformations
• 2 ways
– Object Transformation
• Alter the coordinates descriptions an object
• Translation, rotation, scaling etc.
• Coordinate system unchanged
– Coordinate transformation
• Produce a different coordinate system
Matrix Math
• Why do we use matrix?
– More convenient organization of data.
– More efficient processing
– Enable the combination of various
concatenations
• Matrix addition and subtraction

a c a c
 = b d
b d
Matrix Math
– How about it?
– a  c d
b e f

• Matrix Multiplication
– Dot product
a b e f a.e + b.g a.f + b.h
c d
. g h
=
c.e + d.g c.f + d.h
Matrix Math
• What about this?
1 2 . 1 2 = 6 6
3 1

2 1 2
3
. 3 1 = tak boleh!!

• Type of matrix
a b a
b
Row-vector Column-vector
Matrix Math
• Is there a difference between possible
representations?
a b   e  ae  bf 
    
c d   f  ce df 

a b 
e f    ae cf be df 
c d 

a c 
e f    ae  bf ce df 
b d 
Matrix Math
• We’ll use the column-vector representation for a
point.
• Which implies that we use pre-multiplication of
the transformation – it appears before the point to
be transformed in the equation.
 A B   x   Ax  By 
 C D    y   Cx  Dy 
     

• What if we needed to switch to the other


convention?
Translation
• A translation moves all points
in an object along the same
straight-line path to new
positions.
• The path is represented by a ?
vector, called the translation or
shift vector.
• We can write the components: ty=4
p'x = px + tx
p'y = py + ty (2, 2) tx = 6
• or in matrix form:
P' = P + T
x’ x tx
y’ = y + ty
Rotation
• A rotation repositions
all points in an object
along a circular path in P’
the plane centered at
the pivot point. 

• First, we’ll assume the P


pivot is at the origin.
Rotation
• Review Trigonometry
=> cos  = x/r , sin = y/r
• x = r. cos , y = r.sin 
P’(x’, y’)

=> cos (+ ) = x’/r



•x’ = r. cos (+ )
r
•x’ = r.coscos -r.sinsin y’ P(x,y)
•x’ = x.cos  – y.sin 
 r y

=>sin (+ ) = y’/r x’ x
y’ = r. sin (+ )
•y’ = r.cossin + r.sincos Identity of Trigonometry

•y’ = x.sin  + y.cos 


Rotation
• We can write the components:
p'x = px cos  – py sin 
p'y = px sin  + py cos 

P’(x’, y’)
• or in matrix form:
P' = R • P
  can be clockwise (-ve) or 
counterclockwise (+ve as our
example). y’
P(x,y)
• Rotation matrix
 r y
 cos  sin   
R 
 sin  cos  x
x’
Rotation
• Example
– Find the transformed point, P’, caused by
rotating P= (5, 1) about the origin through an
angle of 90.
 cos  sin    x   x cos  y sin  
 sin     
 cos   y   x sin   y cos 

 5 cos 90  1 sin 90


 
 5 sin 90  1 cos 90

 5 0  1 1
 
 5 1  1 0
  1
 
 5
Scaling
• Scaling changes the size of an
object and involves two scale
factors, Sx and Sy for the x-
P’
and y- coordinates
respectively.
• Scales are about the origin.
• We can write the components:
p'x = sx • px P
p'y = sy • py
or in matrix form:
P' = S • P
Scale matrix as:
 sx 0
S 
0 s y 

Scaling
If the scale factors are in between
0 and 1  the points will be moved
closer to the origin  the object
will be smaller.
• Example :
•P(2, 5), Sx = 0.5, Sy = 0.5
P(2, 5)
•Find P’ ?

P’

Scaling
If the scale factors are in between
0 and 1  the points will be moved
P’
closer to the origin  the object
will be smaller.
• Example :
•P(2, 5), Sx = 0.5, Sy = 0.5
P(2, 5)
•Find P’ ?
•If the scale factors are larger than 1
 the points will be moved away P’

from the origin  the object will be


larger.
• Example :
•P(2, 5), Sx = 2, Sy = 2
•Find P’ ?
Scaling
• If the scale factors are the same,
P’
Sx = Sy  uniform scaling
• Only change in size (as previous
example)
•If Sx  Sy  differential scaling.
•Change in size and shape
•Example : square  rectangle
P(1, 2)
•P(1, 3), Sx = 2, Sy = 5 , P’ ?

What does scaling by 1 do?


What is that matrix called?
What does scaling by a negative value do?
Homogenous Coordinates
y y
w
x 
x

• Let’s move our problem into 3D.


• Let point (x, y) in 2D be represented by point (x, y, 1) in the
new space.
• Scaling our new point by any value a puts us somewhere along
a particular line: (ax, ay, a).
• A point in 2D can be represented in many ways in the new
space.
• (2, 4) ---------- (8, 16, 4) or (6, 12, 3) or (2, 4, 1) or etc.
Homogenous Coordinates
• We can always map back to the original 2D point by dividing
by the last coordinate
• (15, 6, 3) --- (5, 2).
• (60, 40, 10) - ?.

• Why do we use 1 for the last coordinate?

• The fact that all the points along each line can be mapped
back to the same point in 2D gives this coordinate system its
name – homogeneous coordinates.
Matrix Representation
• Point in column-vector:
x
y
1

• Our point now has three coordinates. So our matrix is


needs to be 3x3.
• Translation
 x  1 0 t x   x 
 y  0 1 t    y 
   y  
 1   0 0 1   1 
Matrix Representation
• Rotation

 x  cos  sin  0  x 


 y  sin  cos 0   y 
  
 1   0 0 1  1 

• Scaling

 x  s x 0 0  x 
 y  0 sy 0   y 
  
 1   0 0 1  1 
Composite Transformation
• We can represent any sequence of transformations as
a single matrix.
– No special cases when transforming a point – matrix •
vector.
– Composite transformations – matrix • matrix.

• Composite transformations:
– Rotate about an arbitrary point – translate, rotate, translate
– Scale about an arbitrary point – translate, scale, translate
– Change coordinate systems – translate, rotate, scale

• Does the order of operations matter?


Composition Properties
• Is matrix multiplication associative?
?
– (A.B).C = A.(B.C)
 a b   e f   i j  ae bg af bh   i j
    
 c d   g h   k
   
l  ce dg cf  dh  k

l
  
aei bgi afk bhk aej bgj afl bhl 

 cei dgi cfk dhk cej dgj cfl dhl 

a b    e f  i j   a b  ei  fk ej  fl 
            
c d    g h  k l   c d  gi hk gj  hl 
aei afk bgi bhk aej afl bgj bhl 
 
cei cfk dgi dhk cej cfl dgj dhl 
Composition Properties
• Is matrix multiplication commutative?
?
–A.B=B.A

 a b  e f   ae  bg af  bh
c d   g  
h   ce  dg cf  dh 
  

e f   a b   ea  fc eb  fd 
g     
 h   c d   ga  hc gb  hd 
Order of operations
So, it does matter. Let’s look at an example:
1. Translate 1. Rotate
2. Rotate 2. Translate
Composite Transformation Matrix
• Arrange the transformation matrices in order from right to left.
• General Pivot- Point Rotation
• Operation :-
1. Translate (pivot point is moved to origin)
2. Rotate about origin
3. Translate (pivot point is returned to original position)
T(pivot) • R() • T(-pivot)
1 0 tx cos -sin 0 1 0 -tx
0 1 ty sin cos 0 0 1 -ty
0 0 1 . 0 0 1 . 0 0 1

1 0 tx cos -sin -tx cos+ ty sin


0 1 ty sin cos -tx sin - ty cos
0 0 1 . 0 0 1

cos -sin -tx cos+ ty sin + tx


sin cos -tx sin - ty cos + ty
0 0 1
Composite Transformation Matrix

• Example
– Perform 60 rotation of a point P(2, 5) about a
pivot point (1,2). Find P’?
cos -sin -tx cos+ ty sin + tx x Sin 60 = 0.8660
sin cos -tx sin - ty cos + ty . y
1 Cos 60 = 0.5
0 0 1

0.5 -0.866 -1.0.5 + 2.0.866 + 1 2


0.866 0.5 -1.0.866- 2.0.5 + 2 . 5
1
0 0 1

0.5 - 0.866 2.232 2 -1.098


0.866 0.5 0.134 . 5 = 4.366 P’ = (-1, 4)
0 0 1 1 1
Without using composite
homogenus matrix
• Example
– Perform 90 rotation of a point P(5, 1) about a pivot
point (2, 2). Find P’?
• 1. Translate pivot point by T( tx = -2, ty = -2)
– P(5, 1 )  P’ (3, -1)
• 2. Rotate P ‘ = 90 degree
• P’(3, -1) -- > cos 90 -sin 90 3 = 0 -1 3 = 1
• sin 90 cos 90 -1 1 0 -1 3
• 3. Translate back by pivot point (tx = 2 , ty = 2)
• P”(1, 3 )  P’’’(3, 5)
Composite Transformation Matrix
General Fixed-Point Scaling
Operation :-
1. Translate (fixed point is moved to origin)
2. Scale with respect to origin
3. Translate (fixed point is returned to original position )

T(fixed) • S(scale) • T(-fixed)

Find the matrix that represents scaling of an


object with respect to any fixed point?

Given P(6, 8) , Sx = 2, Sy = 3 and fixed point


(2, 2). Use that matrix to find P’?
Answer
1 0 tx Sx 0 0 1 0 -tx
0 1 ty 0 Sy 0 0 1 -ty
0 0 1 . 0 0 1 . 0 0 1

1 0 tx Sx 0 -tx Sx
Sx 0 -tx Sx + tx
0 1 ty 0 Sy -ty Sy
= 0 Sy -ty Sy + ty
0 0 1 . 0 0 1
0 0 1

x =6, y = 8, Sx = 2, Sy = 3, tx =2, ty = 2

2 0 -2( 2) + 2 6 10
0 3 -2(3) + 2
. 8 = 20
0 0 1 1 1
Composite Transformation Matrix
General Scaling Direction
Operation :-
1. Rotate (scaling direction align with the coordinate axes)
2. Scale with respect to origin
3. Rotate (scaling direction is returned to original position )

R() • S(scale) • R(–)

Find the composite transformation matrix


by yourself !!
Question
• Given P(5, 8) , rotate it by 900 at arbitrary
point (-6,9) and scale it by factor (2,0.5)
Other transformations
Reflection:

 1 0 0
x-axis y-axis
 0  1 0
 
 0 0 1

  1 0 0
 0 1 0
 
 0 0 1
Other transformations
Reflection:

  1 0 0
origin  0 1 0 line x=y
 0  1 0  1 0 0
   
 0 0 1  0 0 1
Other transformations
Shear:

x-direction
 1 shx 0  1 0 0
y-
0 1 0  sh 1 0
direction

 0 0 1
 y
 0 0 1
Coordinate System Transformations
• We often need to transform points from one coordinate system to
another:
1. We might model an object in non-Cartesian space (polar)
2. Objects may be described in their own local system
3. Other reasons: textures, display, etc

You might also like