Assignment No:
Title: Display Pixel
Problem Statement: Writing a C++ class for displaying pixel or point on the screen.
Theory:
A mathematical point (x, y) where x and y are real numbers in an image area. This point can be scan converted at location (x’ , y’) where x’ is integer part of x and y’ is integer part of y.
Scan conversion: converts primitives such as lines, circles, etc. into pixel values.
Introduction to Qt.
Qt is a cross-platform application and UI framework.
Using Qt, you can write applications once and deploy them across desktop, mobile and embedded operating systems without rewriting the source code.
QPainter
Qt’s 2D graphics engine is based on the QPainter class. QPainter can draw geometric shapes
(points, lines, rectangles, ellipses, arcs, chords, pie segments, polygons, and curves) and also pixmaps, images, and text.
QPainter can be used to draw on a "paint device", like a QWidget, a QPixmap or a QImage.
The way the drawing is performed is influenced by QPainter's settings. The three main settings are:
The pen is used for drawing lines and shape outlines. It consists of a color,a width, a line style, a cap style, and a join style.
The brush is the pattern used for filling geometric shapes. It normally consists of a color and a style, but can also be a texture (a pixmap that is repeated infinitely) or a gradient.
The font is used for drawing text. A font has many attributes, including afamily and a point size.
These settings can be modified by calling setPen(), setBrush(), and setFont() with a
QPen, QBrush, or QFont object.
To start painting to a paint device (typically a widget), we simply create a QPainter and pass a pointer to the device.
The paintEvent() is called when a widget is updated. It is where we create the QPainter object and do the drawing.
For example: void MyWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
...
}
QPixmap : The QPixmap class is an off-screen image representation that can be used as a paint device.
Simply call the save () function to save a QPixmap object.
Class : point
Private:
Int x, y;
Public:
Void plot(x,y);
Fig: Pictorial Representation of Class
Mathematical Model:
Point is an ordered pair of numbers (x,y). where x is the horizontal distance from the origin and y is the vertical distance.
Mathematically a point is represented as: Point p = (x, y);
Assignment No:
Title: Line Drawing
Problem Statement: Write a C++ class for a Line drawing method using overloading DDA and
Bresenham’sAlgorithms, inheriting the pixel or point.
Theory:
A line in Computer Graphics typically refers to a line segment, which is portion of straight line that extends indefinitely in opposite directions.
Equation of line is : y = mx + b,
where m = slope of line
b = y intercept of line.
Fig : scan converting line
Line Drawing is accomplished by calculating intermediate point co-ordinates along the line path between two given end points.
Line Drawing Algorithms:
1.
DDA ( digital Differential Algorithm )
2.
Bresenham’s L ine Drawing
A. DDA :
1.
Accept i/p = (x s
, y s
) & (x e
, y e
)
2.
If abs(x e
- x s
)>= abs(y e
- y s
)
Then step = abs(x e
- x s
)
Else step = abs(y e
- y s
)
3.
Find increment in x and y
Δx = (x e
- x s
)/step
Δy = (y e
- y s
)/step
4.
Initialize start point
x= x s
y = x s
5.
Plot(round(x),round(y),1)
X= x + Δx
Y = y + Δy
6.
Repeat step 5 till steps.
B. Bresenham’s Algorithm :
Main Idea: Move across the x-axis by one unit intervals and at each step choose between two different ‘y’ coordinates.
Algorithm
1.
Input the two line end-points, storing the left end-point in (x
0
,y
0
)
2.
Plot the point (x
0
, y
0
)
3.
Calculate the constants Δx, Δy, 2Δy, and (2Δy - 2Δx) and get the first value for the decision parameter as: P
0
= 2∆y – ∆x
4.
At each x k
along the line, starting at k=0, perform the following test:
If p k
< 0, the next point to plot is (x k
+1, y k
) and p k+1
= p k
+ 2∆y
Otherwise, the next point to plot is (x k
+1, y k
+1) and p k+1
= p k
+ 2∆y – 2∆x
5.
Repeat step 4 (Δx ) times
Class point
Protected:
Int x, y;
Public:
Void plot_coor();
Class lines : public point
Private:
Int x1, y1,x2,y2;
Public:
Void get_coor();
Void draw(int);
Void draw();
Fig: Pictorial Representation of Class
Mathematical Model:
Line = set of contiguous points
Ie Line = {<xi,yi> | i>=1, xi-xi+1=0 or 1 }
Mathematical Model of DDA algorithm :
i/p : line segment with endcoordinates : ( Xs, Ys) and (Xe, Ye)
m->slope of the line
|m| <= 1
|m| >1
Xs < Xe
Ys> Ye m is –ve
X i+1
= X i + 1
Y i+1
= y i +m
X i+1
= X i – 1/m
Y i+1
= y i - 1
Xs > Xe
Ys< Ye m is –ve
X i+1
= X i - 1
Y i+1
= y i - m
X i+1
= X i + 1/m
Y i+1
= y i + 1
Xs > Xe
Ys>Ye m is +ve
X i+1
= X i - 1
Y i+1
= y i - m
X i+1
= X i – 1/m
Y i+1
= y i - 1
Mathematical Model of Bresenham’s line drawing algorithm :
i/p : ( Xs, Ys) and (Xe, Ye)
Assumption : line with +ve slope less than 1. A pixel ( Xk, Yk) is plotted on the line in kth step.
A c
B
d1: dist of Pixel ‘A’ from true line path
d2 = dist of Pixel ‘B’ from true line path
C : point on true line path
Pk : error term / Decision variable p0 = 2 Δ y – Δx
Pk > 0 then
X k+1
= X k + 1
Y
k+1
= y k + 1
If Pk <0 then
X k+1
= X k + 1
Y
k+1
= y k
Increment in Error term is
Pk+1 = Pk + 2 Δ y – 2 Δx
PK+1 = Pk + 2 Δ y
if pk >0
if Pk<0
Assignment No:
Title: Circle drawing in Qt creator
Problem Statement:
Theory:
Empty circle using QPainter on QMainWindow : Drawing empty circle on QMainWindow involves subclassing of QMainWindow and overriding paintEvent(QPaintEvent*) method.
Circle in Qt:
Syntax: void QPainter::drawArc(int x, int y, int width, int height, int startAngle, int spanAngle)
This is an overloaded function.
Draws the arc defined by the rectangle beginning at ( x , y ) with the specified width and height , and the given startAngle and spanAngle .
The startAngle and spanAngle must be specified in 1/16th of a degree, i.e. a full circle equals
5760 (16 * 360).
Example :
painter.drawArc( 50 , 30 , 150 , 150 , 0 , 16 * 360 );
//width&height:150px
//Beginning(x/y):50px/30px
Assignment No:
Title: Bresenham’s Circle
Problem Statement: Write a C++ class for a circle drawing inheriting line class.
Theory:
A circle is symmetrical figure. Any circle generating algorithm can take advantage of circle’s symmetry to plot 8 points for each value that the algorithm calculates.
8 –way symmetry is used by reflecting each calculated point around each 45 degree axis.
Fig: 8 way Symmetry of Circle
Defining Circle: A circle is defined as the set of points that are all at a given distance r from a center position (xc,yc).
Circle representation :
Cartesian form : x 2 + y 2 = r 2
Polar form : x = r Cos
y = r Sin
Bresenham’s Circle Drawing Algorithm:
Idea : Increment x by one unit and find corresponding y close to true circle path.
A better algorithm for generating circle has been developed by Bresenham’s . Coordinates on the periphery of a circle which is centered at origin are computed for the 1/8 th part of the circle and remaining pixels are computed by using 8 way symmetry property of the circle.
Algorithm:
1.
read radius r
2.
d= 3 – 2 r
3.
x = 0 and y = r do {
Plot (x , y)
Plot (y , x)
Plot (y , -x)
Plot (x , -y)
Plot (-x , -y)
Plot (-y , -x )
Plot (-y , x ) if d < 0 then
Plot ( -x , y ) d = p i
+ 4x i
+ 6 else if { d ≥ 0 then d = d +
4( x – y ) + 10 y = y
– 1 }
x++;
} while (x <= y)
4.
stop.
Class lines
Protected:
Int x1, y1,x2,y2;
Public:
Void plot_coor();
Class lines : public point
Private:
Int r;
Public:
Void get_coor();
Void draw();
Fig: Pictorial Representation of Class
Mathematical Model: i/p : radius of the Circle (r)
Figure : choosing pixels in Bresenham’s Circle Algorithm
(xi , yi) are the coordinates of the last scan converted pixel upon entering i th step.
D(T) : Dist from the origin to the pixel ‘T’ squared minus the dist to the true circle squared.
D(S) :Dist from the origin to the pixel ‘S’ squared minus the dist to the true circle squared.
Di : error term / Decision variable = D(t) + D(s) d0 =3 - 2 r di < 0 then
X i+1
= X i + 1 and Y
i+1
= y i di >= 0 then
X i+1
= X i + 1 and Y
i+1
= y i -1
Increment in Error term is di+1 = di + 4 X i + 6 if di < 0 di+1 = di + 4(xi – yi ) +10 if di>=0
Assignment No:
Title: Line Styles in Qt Creator.
Problem Statement: Write a program in C/C++ to draw a line with line style (Thick, Thin,
Dotted)
Theory:
A line in Computer Graphics typically refers to a line segment, which is portion of straight line that extends indefinitely in opposite directions.
Equation of line is, y = mx + b,
where m = slope of line
b = y intercept of line.
Fig : scan converting line
Line Drawing with styles
1.
Thick
2.
Dotted
3.
Dashed
Dotted line :
General line drawing algorithm can be modified to display dotted line. Alternate pixels can be plotted to display dotted line.
Dashed line :
In general line drawing algorithm alternate group of pixels are plotted to display dashed line.
Thick Line :
Let us assume a line with co-or (x1,y1) and (x2,y2) and width w.
Upper and lower line boundaries are :
[(x1, y1 + w y
), (x2, y2 + w y
)] and [(x1, y1 - w y
), (x2, y2 - w y
)] respectively.
Algorithm :
1.
Accept line co-ordinates and thickness w
2.
If the slope of the line is <1 then increment x by 1 always and find y
3.
Find wy
4.
Draw parallel line segments from center of the thick line till you reach the bottom and top boundaries.
5.
Stop
Class lines
Private:
Int x1, y1,x2,y2,w;
Public:
Void get_coor();
Void draw_thick();
Void draw_dotted();
Void draw_dash();
Fig: Pictorial Representation of Class
Mathematical Model:
Line = set of contiguous points= {<xi,yi> | i>=1, xi-xi+1=0 or 1 }
1.
Thick Line
Thick line ={ {line } , w}