Writing a C++ class for displaying pixel or point on the screen.

advertisement

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.

intro to graphics in tc++ :

To initialize graphics mode initgraph() function is used in program. initgraph function is present in "graphics.h" header file, so every graphics program should include "graphics.h" header file.

Syntax: initgraph ( & gd , & gm , "C: \\ TC \\ BGI" ) ;

Initgraph : this function automatically decides an appropriate graphics driver and mode such that maximum screen resolution is set. Three arguments are passed to initgraph() function first is the address of gd, second is the address of gm and third is the path where your BGI files are present.

Integer gd is set to DETECT. DETECT is a macro defined in "graphics.h" header file.

Closegraph() function closes the graphics mode.

Class : point

Private:

Int x, y;

Public:

Void get_coor();

Void plot_coor();

Fig: Pictorial Representation of Class

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

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 )

Plot ( -x , y ) if d < 0 then 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();

Fig: Pictorial Representation of Class

Class lines : public point

Private:

Int r;

Public:

Void get_coor();

Void draw();

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

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

...

}

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

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.

Assignment No:

Title: Polygon drawing in Java

Problem Statement: Write a Java program to draw a simple polygons (Square, Rectangle,

Triangle)

Theory:

Polygon : It is a closed polyline.

Types of Polygon :

1.

Convex Polygon : A convex polygon is a polygon such that for any two points inside the polygon, all points on the line segment connecting them are also inside the Polygon.

2.

Concave Polygon : A concave polygon is a polygon such that for any two points inside the polygon, if some points on the line segment connecting them are not inside the

Polygon.

Graphics in java : To do custom graphics in a JAVA application, write a new class that extends the JPanel class. In that class, override the definition of the paintComponent() method.

A Custom Graphics Template: import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.swing.event.*; public class ClassName extends JPanel {

public void paintComponent(Graphics g) { super.paintComponent(g);

}

}

Java Swing:

It is java framework. Java Swing is a lightweight Java graphical user interface (GUI) widget toolkit that includes a rich set of widgets. It includes several packages for developing rich desktop applications in Java. Swing includes built-in controls such as trees, image buttons, tabbed panes, sliders, toolbars, color choosers, tables, and text areas to display HTTP or rich text format (RTF).

Scanner class simplifies console input. The Scanner class is a class in java.util, which allows the user to read values of various type.

1.

import java.awt.* : AWT stands for Abstract Window ToolKit.

The Abstract Window

Toolkit provides many classes for programmers to use. It is the connection between the application and the GUI. It contains classes that programmers can use to make graphical components, e.g., buttons, labels, frames

2.

import.java.util.* : The java.util package contains classes that deal with collections, events, date and time, internationalization and various helpful utilities.

3.

Import.java.swing.* : Swing is built on top of AWT, and provides a new set of more sophisticated graphical interface components.

4.

Import.java.swing.event.* : This package defines classes and interfaces used for event handling in the AWT.

Here Jpanel is the canvas on which drawing is done. paintComponent() method is pre-defined. If we want to draw something on the panel, then you need to override it.

A Graphics object g controls the visual appearance of a Swing component.

Graphics object is obtained as a parameter to the paintComponent() method. Once you have the

Graphics object, you can send it messages to change the color or font that it uses, or to draw a variety of geometric figures. super.paintComponent(g) invokes the paintComponent method from the superclass of JPanel

(the JComponent class) to erase whatever is currently drawn on the panel. This is useful for animation.

JFrame:

A Frame is a top-level window with a title and a border. A frame, implemented as an instance of the JFrame class, is a window that has decorations such as a border, a title, and supports button components that close or magnify the window. Applications with a GUI usually include at least one frame.

Title: Booths multiplication

Problem statement: Write a class to implement the Booths Multiplier for 8/16/32/64-bit numbers using sign extended multiplication.

Theory:

Points to remember:

When using Booth's Algorithm:

You will need twice as many bits in your product as you have in your original two operands .

The leftmost bit of your operands (both your multiplicand and multiplier) is a

SIGN bit, and cannot be used as part of the value.

To begin:

Decide which operand will be the multiplier and which will be the multiplicand

Convert both operands to two's complement representation using X bits

X must be at least one more bit than is required for the binary representation of the numerically larger operand

Begin with a product that consists of the multiplier with an additional X leading zero bits

Algorithm:

Step 1: Load A=0, Q

-1

= 0

B = Multiplicand

Q = Multiplier and SC = n

Step 2: Check the status of Q

0

Q

-1 if Q

0

Q

-1

= 10 perform A = A-B if Q

0

Q

-1

= 01 perform A = A+B

Step 3: Arithmetic shift right : A,Q,Q

-1

Step 4: Decrement sequence counter if not zero, repeat step 2 through 4

Step 5: Stop

Flowchart:

Assignment No:

Title: Line drawing in VRML

Problem Statement:

Theory:

Download