Title: Computer Graphics Rendering 2D Geometry
1Computer GraphicsRendering 2D Geometry
- CO2409 Computer Graphics
- Week 2
2Todays Lecture
- 2D Geometry
- Coordinate Systems Viewports
- Rendering Points and Lines
- Polygons and Circles
- Anti-Aliasing
32D Geometry Basics
- Will look at 2D geometry represented by points
and lines in two dimensional Cartesian space. - Represented on a graph with two axes, usually X
(horizontal) and Y (vertical) - Origin of the graph and of the 2D space is where
the axes cross
42D Geometry Definitions
- I will often use informal English to describe 2D
geometry. However, here are a couple of specific
definitions that I will use
A vertex a single point (plural vertices)
defined by coordinates on the axes An edge a
straight line joining two vertices A polygon a
single closed loop of edges
5Specifying 2D Geometry
- Example vertices
- A(10, 20), B(30, 30), C(35, 15)
- Edges
- AB, BC, AC
- Edges with direction (a form of vector, see later
lectures) - AB, BC, CA
- Polygons
- ABCA, or ABC (implies the final edge to A)
6Coordinate Systems
- Define a coordinate system as a particular set of
choices for - The location of the origin
- The orientation and scale of the axes
- A vertex may have different coordinates in two
different coordinate systems
7Viewports
- A viewport (or window) is a rectangle of pixels
representing a region of 2D geometry space. - A viewport has its own coordinate system, which
may not match that of the geometry. - The axes will usually be X horizontal Y
vertical - But dont have to be rotated viewports
- The scale of the axes may be different
- The direction of the Y axis may differ.
- E.g. the geometry may be stored with ve Y up,
but the viewport has ve Y down. - The origin (usually in the corners or centre of
the viewport) may not match the geometry origin.
8Viewport Example
- Example of changing coordinate system from
geometry space to viewport space
P (20,15) in geometry space. Where is P in
viewport space?
9Rendering Points
- Rendering is the process of converting geometry
into pixels - To render a vertex (a point)
- Convert vertex coordinates into viewport space
- Set the colour of the pixel at those coordinates
- The colour might be stored with the geometry, or
we can use a fixed colour (e.g. black) - In the previous diagram the vertex P would be
rendered by setting the colour of the pixel at
coordinates (266, 450) in the viewport
10Rendering Lines 1st Attempt
- A possible method to render an edge (a line)
- Convert the start and end vertex into viewport
space - Trace along this converted edge
- Colour each pixel traced over
- Unfortunately, this method creates thick lines.
- We need to colour fewer pixels to get a cleaner
result.
11Rendering Lines 2nd Attempt
- Consider drawing a horizontal line with previous
method - Trace along horizontally adjacent pixels, setting
the colour of each. Result is fine. - Adapt this for a diagonal line
- Start at the left end of the line
- Move rightwards, colouring each pixel
- Periodically move up/down one pixel to create
steps of the correct gradient - Use variations of above for lines in different
directions
To go from start to end need to move right 9
pixels and down 4 pixels. So move down one pixel
about every 2 pixels right
12Rendering Lines Algorithms
- We basic version of this algorithm to render
lines - Using floating point calculations
- Most widely used method is Bresenhams Line
Algorithm - Highly efficient - only uses integer calculations
- We wont cover this, or other efficient 2D
algorithms in this module instead concentrate
on higher level graphics work - Many 2D algorithms are implemented in hardware
nowadays
13Rendering Polygons
- A polygon is a sequence of edges, so we simply
need to render each edge. - We only need the set of vertices defining the
polygon - Squares and rectangles are special cases of a
polygon and can be rendered more efficiently. - Horizontal / vertical lines are simple cases
- Will not consider filled polygons until the 3D
section of the module.
14Rendering Circles?
- The coordinates of a circle can be written as
- X A R sin(a), Y B R cos(a), for 0 a lt
360º - where (A, B) is the circle centre and R the
radius - We could use this to render a circle
- Step a from 0 to 360 and calculate X Y
- Convert to viewport space and colour the pixels
- Not ideal though try it in the lab
- Again there is a better integer based method from
Bresenham
15Regular Polygons
- Can use the circle equations to create regular
polygons. - A regular polygon has all its points on a circle
- Can calculate the vertices using the circle
equations - Conversely - can we use regular polygons to draw
a circle?
16Anti-Aliasing
- Returning to the naive line algorithm
- Again we colour every pixel passed through
- This time consider how close the centre of the
pixel is to the line we are drawing - The colour used is more intense for pixels that
lie nearer the line
- This is an anti-aliased line
- Aliasing is the jagged look of pixel edges
- This method smoothes these effects hence the
name anti-aliasing