1. Introduction
Definition:
Displaying 2D geometric objects involves drawing basic shapes such as points, lines, circles, and polygons on a 2D plane using computer graphics systems.
These are fundamental building blocks for graphics applications like CAD, UI design, plotting, and games.
Key Concept:
2D graphics rely on a coordinate system, typically the Cartesian (x,y) plane, to position and render shapes.
2. Coordinate Systems in 2D Graphics
-
World Coordinates:
- Defines positions relative to a logical scene
- Independent of the screen or device resolution
-
Device Coordinates:
- Maps world coordinates to actual screen pixels
- Conversion from world → device coordinates is necessary for rendering
-
Viewport:
- Specifies the rectangular portion of the window where objects are drawn
3. Basic 2D Geometric Primitives
A. Point
- Definition: The simplest object in 2D graphics, defined by a single coordinate (x,y).
- Usage: Plotting data, vertices of polygons, or markers.
B. Line
-
Definition: A straight connection between two points (x1,y1) and (x2,y2).
-
Drawing Algorithms:
- DDA (Digital Differential Analyzer): Incremental algorithm using floating-point arithmetic.
- Bresenham’s Line Algorithm: Efficient integer-based algorithm for raster devices.
C. Circle
-
Definition: Set of points equidistant from a center (xc,yc) with radius r.
-
Equation: (x−xc)2+(y−yc)2=r2
-
Drawing Algorithms:
- Midpoint Circle Algorithm
- Bresenham’s Circle Algorithm
D. Ellipse
- Definition: Set of points where the sum of distances from two foci is constant.
- Equation: a2(x−xc)2+b2(y−yc)2=1
- Drawing Algorithm: Midpoint Ellipse Algorithm
E. Polygon
-
Definition: Closed shape made of lines connecting a sequence of points.
-
Types: Triangles, rectangles, pentagons, etc.
-
Filling Algorithms:
- Scanline fill algorithm
- Flood-fill algorithm
4. Transformations of 2D Objects
To manipulate 2D shapes, several transformations are applied:
- Translation: Move object by (dx,dy)
x′=x+dx,y′=y+dy
- Scaling: Resize object relative to origin or a reference point
x′=Sx⋅x,y′=Sy⋅y
- Rotation: Rotate object around the origin by angle θ
x′=xcosθ−ysinθ,y′=xsinθ+ycosθ
-
Reflection: Mirror object along x-axis, y-axis, or a line
-
Shearing: Skew object along x or y-axis
5. Rendering 2D Objects
Steps to Display:
- Convert world coordinates to device coordinates.
- Apply transformation matrices if needed.
- Use a rasterization algorithm to generate pixels.
- Store pixels in the framebuffer and display on the screen.
Example in OpenGL (Pseudocode):
glBegin(GL_TRIANGLES);
glVertex2f(0.0, 0.0);
glVertex2f(0.5, 0.0);
glVertex2f(0.25, 0.5);
glEnd();
glFlush();
6. Applications
- User Interface Elements: Buttons, sliders, icons
- Computer-Aided Design (CAD): Basic drafting and modeling
- Graph Plotting: Charts, graphs, and scientific visualization
- Games: Sprites, backgrounds, and simple 2D animations
Key Takeaways
- 2D geometric objects are the building blocks of computer graphics.
- Efficient algorithms are used for drawing primitives like lines, circles, and polygons.
- Transformations allow manipulation of objects for animation, design, and interaction.
- Mastering 2D graphics is essential before moving to 3D graphics and rendering pipelines.