Advantages of Interactive Graphics
Interactive computer
graphics means that the user can control and change the picture on the screen.
The user can see the image and give commands using an input device like a
mouse, keyboard, or touch screen. This creates two-way communication between
the computer and the user.
Benefits of
Interactive Graphics
1. Better Quality: Users can create and change images with
great accuracy, which leads to higher quality results.
2. More Accurate Output: Users can make small adjustments and
fine-tune their designs, resulting in more precise final products.
3. Higher Productivity: Tasks get done faster because users can
see changes instantly and fix mistakes right away.
4. Lower Design and Analysis Costs: By seeing designs and data visually,
mistakes can be found early, saving money on rework and physical models.
5. Better Understanding of Data: Graphs and charts make it easier to
understand complex information and spot patterns or trends.
6. Easy to Use: Graphical interfaces are attractive and simple to use,
which makes computer systems more popular and successful.
Common Uses of Computer Graphics
Computer graphics is used in many fields such
as industry, business, government, education, entertainment, and homes.
Easy-to-use graphical interfaces are one of the main reasons for the success of
any computer system.
Where Computer Graphics is Used
1. Business and Industry: Computer graphics is often used to
create 2D and 3D charts like bar charts, histograms, and pie charts. These
charts help managers make better decisions.
2. Desktop Publishing: On personal computers, desktop
publishing lets users create documents with text, tables, graphs, and pictures.
This helps in office automation and sharing information.
3. Computer-Aided Design (CAD): Graphics are used to design parts and
systems for cars, buildings, airplanes, ships, computer chips, and electronic
devices.
4. Simulation: Using graphics in simulation makes
mathematical models and mechanical systems look more real and easier to study.
Animation software is used to make cartoon movies.
5. Art and Advertising: Graphics tools let users create artistic
pictures that send messages and catch people's attention. Such pictures are
very useful in ads and marketing.
Conceptual Framework for Interactive Graphics
The conceptual framework has four main parts:
1. Graphics Library (API): This sits between the application
program and the display hardware. It provides functions for drawing shapes,
setting colors, and handling user input.
2. Application Program: This program turns application objects
into images by calling graphics library functions.
3. Graphics System: This is the interface that connects the
graphics library to the hardware (monitor and graphics card).
4. User Interaction: Changes to images happen when the user
interacts with the system using input devices.
Scan Converting a Straight Line (Drawing a
Line on Screen)
A straight line is defined by two endpoints:
(x1, y1) and (x2, y2). The line equation is used to find all the x, y points
that lie between these two endpoints.
The equation of a straight line is: y
= m x + b
Where:
·
m
= (y2 - y1) / (x2 - x1) (this
is the slope of the line)
·
b
= y1 - m × x1 (this is the
y-intercept)
By calculating these (x, y) values and putting
pixels on the screen, we draw the line as a sequence of dots.
Steps to Draw a Line Using the Equation Method
Step 1: Start the process.
Step 2: Declare the variables: x1, x2, y1, y2, dx, dy, m, b.
Step 3: Input the values of x1, x2, y1, y2 (the start and end
points of the line).
Step 4: Calculate dx = x2 - x1
Step 5: Calculate dy = y2 - y1
Step 6: Calculate m = dy / dx
Step 7: Calculate b = y1 - m × x1
Step 8: Set the starting point and ending point:
·
If dx is less than 0:
o Set x = x2, y = y2, and xend = x1
·
If dx is greater than
0:
o Set x = x1, y = y1, and xend = x2
Step 9: Check if the whole line is drawn. If x equals xend, then
stop.
Step 10: Put a pixel at the current (x, y) position.
Step 11: Increase x by 1: x = x + 1
Step 12: Calculate the next y value using: y = m × x + b
Step 13: Go back to Step 9.
C++ Program to Draw a Line Using the Slope
Method
cpp
Copy
Download
#include <graphics.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
class LineDrawer
{
float x, y, x1, y1, x2, y2, dx, dy, m, c, xend;
public:
void get();
void draw();
};
void main()
{
LineDrawer ld;
ld.get();
ld.draw();
getch();
}
void LineDrawer::get()
{
printf("Enter start and end points\n");
printf("Enter x1, y1, x2, y2\n");
scanf("%f%f%f%f", &x1, &y1, &x2, &y2);
}
void LineDrawer::draw()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, " ");
errorcode = graphresult();
if (errorcode != grOK)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to stop:");
getch();
exit(1);
}
dx = x2 - x1;
dy = y2 - y1;
m = dy / dx;
c = y1 - (m * x1);
if (dx < 0)
{
x = x2;
y = y2;
xend = x1;
}
else
{
x = x1;
y = y1;
xend = x2;
}
while (x <= xend)
{
putpixel(x, y, RED);
x++;
y = (m * x) + c;
}
}
Scan Converting a Circle (Drawing a Circle on
Screen)
A circle has eight-way symmetry. This means if
we find a point in one part of the circle, we can easily find seven more points
using reflection.
If we have a point P(x, y) on a circle
centered at the origin, the other seven points are:
·
P(x, -y)
·
P(-x, y)
·
P(-x, -y)
·
P(y, x)
·
P(y, -x)
·
P(-y, x)
·
P(-y, -x)
Displaying Eight Symmetric Points
To draw a circle, we put pixels at these eight
positions:
text
Copy
Download
putpixel(x + h, y + k,
color) // point in first quadrant
putpixel(x + h, -y +
k, color) // point in fourth quadrant
putpixel(-x + h, -y +
k, color) // point in third quadrant
putpixel(-x + h, y +
k, color) // point in second quadrant
putpixel(y + h, x + k,
color) // rotated point in first
quadrant
putpixel(y + h, -x +
k, color) // rotated point in fourth
quadrant
putpixel(-y + h, -x +
k, color) // rotated point in third
quadrant
putpixel(-y + h, x +
k, color) // rotated point in second
quadrant
Here, (h, k) is the center of the circle.
Example: If we find a point (2, 7), the other points will be:
(2, -7), (-2, -7), (-2, 7), (7, 2), (-7, 2), (-7, -2), (7, -2)
Two Ways to Define a Circle
1. Polynomial Method: Using the formula x² + y² = r²
2. Polar Coordinate Method: Using x = r × cos(θ) and y = r × sin(θ)
Program to Draw a Circle Using the Polynomial
Method
cpp
Copy
Download
#include <graphics.h>
#include <conio.h>
#include <math.h>
void plotCirclePoints(int x, int y, int h, int k)
{
putpixel(x + h, y + k, RED);
putpixel(x + h, -y + k, RED);
putpixel(-x + h, -y + k, RED);
putpixel(-x + h, y + k, RED);
putpixel(y + h, x + k, RED);
putpixel(y + h, -x + k, RED);
putpixel(-y + h, -x + k, RED);
putpixel(-y + h, x + k, RED);
}
void main()
{
int gd = DETECT, gm, h, k, r;
double x, y, x2;
h = 200; // center x
k = 200; // center y
r = 100; // radius
initgraph(&gd, &gm, "C:\\TC\\BGI");
setbkcolor(WHITE);
x = 0;
y = r;
x2 = r / sqrt(2);
while (x <= x2)
{
y = sqrt(r * r - x * x);
plotCirclePoints(floor(x), floor(y), h, k);
x = x + 1;
}
getch();
closegraph();
}
Program to Draw a Circle Using Polar
Coordinates
cpp
Copy
Download
#include <graphics.h>
#include <stdlib.h>
#define COLOR 10
void plotEightPoints(int xc, int yc, int x, int y)
{
putpixel(x + xc, y + yc, COLOR);
putpixel(x + xc, -y + yc, COLOR);
putpixel(-x + xc, -y + yc, COLOR);
putpixel(-x + xc, y + yc, COLOR);
putpixel(y + xc, x + yc, COLOR);
putpixel(y + xc, -x + yc, COLOR);
putpixel(-y + xc, -x + yc, COLOR);
putpixel(-y + xc, x + yc, COLOR);
}
void drawPolarCircle(int xc, int yc, int r)
{
int x, y, d;
x = 0;
y = r;
d = 3 - 2 * r;
plotEightPoints(xc, yc, x, y);
while (x <= y)
{
if (d <= 0)
{
d = d + 4 * x + 6;
}
else
{
d = d + 4 * x - 4 * y + 10;
y = y - 1;
}
x = x + 1;
plotEightPoints(xc, yc, x, y);
}
}
int main()
{
int gdriver = DETECT, gmode, errorcode;
int xc, yc, r;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to stop:");
getch();
exit(1);
}
printf("Enter center x and y: ");
scanf("%d%d", &xc, &yc);
printf("Enter radius: ");
scanf("%d", &r);
drawPolarCircle(xc, yc, r);
getch();
closegraph();
return 0;
}
Scan Converting an Ellipse (Drawing an Ellipse on Screen)
An ellipse is also symmetric, but it has four-way
symmetry (not eight-way like a circle). This means if we find a point
in one quadrant, we can reflect it to the other three quadrants.
Program to Draw an Ellipse
cpp
Copy
Download
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void plotEllipsePoints();
float x, y;
int xc, yc;
void main()
{
int gd = DETECT, gm, a, b;
float p1, p2;
clrscr();
initgraph(&gd, &gm, "c:\\turboc3\\bgi");
printf("*** Ellipse Drawing Algorithm
***\n");
printf("Enter center x: ");
scanf("%d", &xc);
printf("Enter center y: ");
scanf("%d", &yc);
printf("Enter x-axis length: ");
scanf("%d", &a);
printf("Enter y-axis length: ");
scanf("%d", &b);
x = 0;
y = b;
plotEllipsePoints();
p1 = (b * b) - (a * a * b) + (a * a) / 4;
while ((2.0 * b * b * x) <= (2.0 * a * a * y))
{
x++;
if (p1 <= 0)
{
p1 = p1 + (2.0 * b * b * x) + (b * b);
}
else
{
y--;
p1 = p1 + (2.0 * b * b * x) + (b * b) - (2.0 * a * a * y);
}
plotEllipsePoints();
x = -x;
plotEllipsePoints();
x = -x;
delay(50);
}
x = a;
y = 0;
plotEllipsePoints();
p2 = (a * a) + 2.0 * (b * b * a) + (b * b) / 4;
while ((2.0 * b * b * x) > (2.0 * a * a * y))
{
y++;
if (p2 > 0)
{
p2 = p2 + (a * a) - (2.0 * a * a * y);
}
else
{
x--;
p2 = p2 + (2.0 * b * b * x) - (2.0 * a * a * y) + (a * a);
}
plotEllipsePoints();
y = -y;
plotEllipsePoints();
y = -y;
delay(50);
}
getch();
closegraph();
}
void plotEllipsePoints()
{
putpixel(xc + x, yc + y, 7);
putpixel(xc - x, yc + y, 7);
putpixel(xc + x, yc - y, 7);
putpixel(xc - x, yc - y, 7);
}