Vector Graphics
A Simplified Approach with Examples in Java
|
Prof. David Bernstein
James Madison University
|
|
Computer Science Department
|
bernstdh@jmu.edu
|
Representation of Visual Content
- Raster Representation:
- Content is thought of as a grid/matrix
(spatial discretization)
- Each picture element (i.e., pixel) contains one color
- Set of colors is determined by the palette (color
quantization)
- Vector Representation:
- Content is thought of as a set of
shapes (e.g., points, lines, polygons/rectangles/ovals)
- Shapes are defined using intension, not extension
Rendering of Visual Content
- Purpose:
- Present visual content on an output device
- Devices:
- Discrete rendering (e.g., LCD panels have physical pixels)
- Continuous rendering (e.g., pen plotters)
Some Observations
- The representation can be (and usually is) independent of the
renderer
- We are concerned only with the representation
Simplified Vector Graphics
- Concepts:
- Paper - the material being drawn on
- Pen - the instrument used to do the drawing
- Ink - the material in the pen that changes the color of
the paper
- Operations:
-
setPaperColor(name:String)
-
setInkColor(name:String)
-
setPenWidth(pixels:int)
-
setLineType(name:String)
-
moveTo(p:Point)
-
drawTo(p:Point)
Simplified Vector Graphics (cont.)
Coordinates
A Vector Graphics Class
Drafter
Required of the Analytic Geometry Classes
- The
Point
Class:
- Must have a method
getCoordinate(i:int):double
- The indexes must be 0-based
- Other Classes:
- Should have a method that returns an iterator of
Point
objects
A ConcreteDrafter
A Floor Plan
javaexamples/navigation/FloorPlanExample.java
Drawing Features
LineSegment
javaexamples/navigation/FeatureDrafter1.java
(Fragment: 1)
Drawing Features (cont.)
PiecewiseLinearCurve
javaexamples/navigation/FeatureDrafter1.java
(Fragment: 2)
Drawing Features (cont.)
Polygon
javaexamples/navigation/FeatureDrafter1.java
(Fragment: 3)
Drawing Features (cont.)
- The code for
PiecewiseLinearCurve
and
Polygon
are identical.
- The code for
LineSegment
can be modified
to work the same way.
- It would be nice to be able to draw a
Point
.
Drawing Features (cont.)
All Classes that Implement Feature
javaexamples/navigation/FeatureDrafter2.java
(Fragment: 1)
What About Re-Sizing?
- Scaling:
- Multiplying each
Point
in the
Feature
by a scalar
- Translation:
- Adding to each
Point
in the
Feature
What About Re-Sizing? (cont.)
Coordinates
Scaling
- Determine the Range in World Coordinates:
- \(\rho = w^{\mbox{max}} - w^{\mbox{min}}\)
- Determine the Scale for Each Coordinate:
- \(\sigma_{1} = s_{1}^{\mbox{max}} / \rho_{1}\)
- \(\sigma_{2} = s_{2}^{\mbox{max}} / \rho_{2}\)
- Maintain the Aspect Ratio:
- \(\alpha = \min(\sigma_{1}, \sigma_{2})\)
- Scale Each Point:
Scaling (cont.)
- Methods in
Point
:
- Add and subtract
Point
objects
- Methods to Add to
Point
?
- Component-by-component multiplication/division
- Smallest/largest component in a
Point
Translating the Scaled Points
- Scale the Minimum in World Coordinates:
- \(\hat{w}^{\mbox{min}} = \alpha w^{\mbox{min}}\)
- Translate the Scaled Point:
- \(q = \hat{p} - \hat{w}^{\mbox{min}}\)
Translating the Scaled Points (cont.)
- Methods in
Point
:
- Multiply a scalar and a
Point
object
- Add and subtract
Point
objects
- Methods to Add to
Point
?
The Rectangular Hull
- Defined:
- The smallest rectangle that contains a set of points
- Software Design Considerations:
- Can be represented as two
Point
objects,
the component-by-component minimum and maximum
- Should be able to "adjust itself" for a new
Point
object or another RectangularHull
object
- Static Methods to Add to
Point
?
- The component-by-component min and max of two
Point
objects
The Rectangular Hull (cont.)
- Methods to add to
Feature
:
-
getRectangularHull():RectangularHull
- Software Design Considerations:
- Could be in an abstract parent
but a particular class (i.e.,
LineSegment
,
PiecewiseLinearCurve
, etc...) may be able to
do it more efficiently
The Rectangular Hull (cont.)
One Possible Implementation
javaexamples/navigation/RectangularHull.java
Drawing Re-sized Features
javaexamples/navigation/FeatureDrafter3.java
(Fragment: 1)
There's Always More to Learn