Sunteți pe pagina 1din 34

2D and 3D Geometry

Finding Nemo © Disney/Pixar. The Lord of the Rings © New Line Productions, Inc. Geri’s Game © Pixar.
This Section’s Objectives

• Define the basic elements of geometry


– Scalars
– Vectors
– Points

• Develop mathematical operations among them in a


coordinate-free manner

• Define
D fi b basic
i graphics
hi primitives
i iti
– Line segments
– Polygons

CSCI 3161- Computer Animation


Basic Elements

• Geometry is the study of the relationships


among objects
bj iin an n-dimensional
di i l space
– we are interested in objects that exist in (at most) 3D

• We want a minimum set of primitives from which


we can build more sophisticated objects

• We will need three basic elements


– Scalars
– Vectors
– Points

CSCI 3161- Computer Animation


Coordinate-Free Geometry

• When we learned simple geometry, most of us


started with a Cartesian approach
– Points were at locations in space P=(x,y,z)
– We derived results by algebraic manipulations
involving these coordinates

• Most geometric results are independent of the


coordinate system
– Euclidean geometry: two triangles are identical if two
corresponding sides and the angle between them are
identical

CSCI 3161- Computer Animation


Scalars

• Scalars are members of sets that can be combined


b addition
by dditi andd multiplication
lti li ti obeying
b i some
fundamental axioms
– associativity : x * (y * z) = (x * y) * z
– commutativity : x +y=y+x
– distributivity : x * (y + z) = (x * y) + (x * z)
– additive inverse : x + (-x)
( x) = 0
– multiplicative inverse : x * (1/x) = 1

• Example: real numbers


• Scalars alone have no geometric properties

CSCI 3161- Computer Animation


Vectors

• A vector is an object with two attributes


– Direction
– Magnitude

• Examples include v
– Force
– Velocity
– Directed
Di t d li
line segments
t
• Most important example for graphics

CSCI 3161- Computer Animation


Vector Operations

• Every vector has an inverse


– Same magnitude but points in opposite direction

• Every vector can be multiplied by a scalar


• There is a zero vector
– Zero magnitude, undefined orientation

• The sum of any two vectors is a vector

v -v 0.7v v w

CSCI 3161- Computer Animation


Linear Vector Spaces

• Mathematical system for manipulating vectors

• Operations
– Scalar-vector multiplication : u = αv
– Vector-vector addition : w = u+v

• Expressions such as v=u+2w-3r make sense in a vector space

• Magnitude of a vector
– Calculate length with Pythagorean theorem

• Normalizing a vector
– Form a unit vector

CSCI 3161- Computer Animation


Linear Combinations & Independence

• A linear combination of n vectors u1, u2,…,un is the


vector of the form:
u = α1u1 + α2u2 +…+ αnun

• If the only set of scalars that produces

α1u1 + α2u2 +…+


+ + αnun = 0
is

α1 = α2 =…= αn = 0

then the vectors are said to be linearly independent

CSCI 3161- Computer Animation


Basis Vectors & Representation

• If a vector space has dimension n, any set of n linearly


independent vectors forms a basis

• If u1, u2,…,un is a basis for vector space, V, any vector v can


be expressed uniquely as a linear combination for the basis
vectors:

v = α1u1 + α2u2 +…+ αnun

• The scalars αi give the representation of v with respect to the basis


u1, u2,…,un :
⎡α 1 ⎤
⎢α ⎥
[α1 α 2 K α n ] = ⎢ 2 ⎥
T

⎢M⎥
⎢ ⎥
⎣α n ⎦
CSCI 3161- Computer Animation
Inner (Dot) Product

• The inner (dot) product between vectors u and v in an


n-dimensional
dimensional space is defined as:
n

∑(u * v )
i =1
i i

• if u ⋅ v = 0 , then u and v are orthogonal (right


angles
g in 2D)

• length of vector u is: u = u ⋅ u

• can use the dot product to define the angle (θ )


between two vectors:
u ⋅ v = u v cos((θ )

CSCI 3161- Computer Animation


Projections of Vectors

• The dot product can also be used to project vector


A onto vector B, yielding a new vector C

C = uB ( A⋅ uB )
where

uB is the unit vector of B

CSCI 3161- Computer Animation


Projecting Vector A on Vector B

// include the library header files


#include <gl/freeglut.h>
#include <stdlib.h>
#include <math
<math.h>
h>

// a point data type


typedef
yp GLfloat p
point2[2];
[ ]

// the vectors
point2 vectorA = {0.0, 0.0};
point2 vectorB = {0.0, 0.0};
point2 vectorC = {0.0, 0.0};

CSCI 3161- Computer Animation


Projecting Vector A on Vector B
void initializeGL(void)
{
// enter GL_PROJECTION mode
glMatrixMode(GL_PROJECTION);

// clear
l th
the projection
j ti matrix
t i
glLoadIdentity();

// set window mode to 2D orthographic


gluOrtho2D(-1.0, 1.0, -1.0, 1.0);

// setup
p the vectors
setupVectors();

CSCI 3161- Computer Animation


Projecting Vector A on Vector B
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_LINES); // draw the lines

// draw vector A red


glColor3f(1,
lC l 3f(1 0 0, 0)
0);
glVertex2f(0, 0);
glVertex2fv(vectorA);

// d
draw vector
t B green

// draw vector C blue


glEnd();

glFlush();
lFl h()
}

CSCI 3161- Computer Animation


Example – projectVector.c

CSCI 3161- Computer Animation


Vectors Lack Position

• These vectors are identical:


– Same length and magnitude

• Vectors
Vecto s spaces are
a e insufficient
su c e t for
o constructing
co st uct g
geometric objects
– We need points too

CSCI 3161- Computer Animation


Points
• Location in space
• Operations
O i allowed
ll dbbetween points
i and
d
vectors
– Point-point
Point point subtraction yields
ields a vector
ector
– Point-vector addition yields a point

v v= P-Q
Q

P = v+Q

CSCI 3161- Computer Animation


Affine Spaces

• Point + a vector space

• Operations
p
– Vector-vector addition
– Scalar-vector multiplication
– Scalar-scalar operations
– Point-vector addition
– Point-Point subtraction

CSCI 3161- Computer Animation


Lines

• Consider all points of the form: P(α)=P0 + α v


– Set of all points that pass through P0 in the
direction of the vector v or -v

P(α)

P0
P(-α)

CSCI 3161- Computer Animation


Rays

• For α ≥ 0, P(α) is the ray leaving P0 in


the direction v :

P(α)

P0

CSCI 3161- Computer Animation


Line Segments

• For 0 ≥ α ≤ 1 we get all the points on the


line segment joining P0 and P(1.0) :

P(1 0)
P(1.0)
v

P0

CSCI 3161- Computer Animation


Parametric Lines - Interpolation

• Nice since it extends to curves and


surfaces
• Forms
– Explicit:
y = mx + h
– Parametric: P1= (x1, y1)
x(α) = αx0 + (1-α)x1
y(α) = αy0 + (1-α)y
(1 α)y1
P0= (x0, y0)

CSCI 3161- Computer Animation


Convexity

• An object is convex iff:


– for any two points in the object all points on the line
segment between these points are also in the object

P
P

Q
Q

convex non-convex
non convex

CSCI 3161- Computer Animation


Triangles

• 3 sided polygon

• Can be defined by:


– 3 points
– 2 points and 2 angles

CSCI 3161- Computer Animation


Triangles – Bi-linear Interpolation

interpolation of
P and Q

for 0 <= α <= 1 and 0 <= β <= 11, we get all points in triangle

CSCI 3161- Computer Animation


Triangles – Bi-linear Interpolation

interpolation of
S(α) and R
α, β

for 0 <= α <= 1 and 0 <= β <= 11, we get all points in triangle

CSCI 3161- Computer Animation


Polygons

• Definition: a closed plane figure bounded by


straight sides
• Can always be broken up into triangles
– Triangles are often easier to deal with

CSCI 3161- Computer Animation


Polygons

• Special names of common polygons:

triangle A polygon with three sides


quadrilateral A polygon with four sides
pentagon A polygon with five sides
hexagon A polygon with six sides
heptagon A polygon with seven sides
octagon
t A polygon
l with
ith eight
i ht sides
id
n-gon A polygon with n sides

CSCI 3161- Computer Animation


Planes in 3D

• A plane can be determined by:


– a point (R) and two vectors (m and n)

R
n

P( β) = R + αm + βn
P(α, β

CSCI 3161- Computer Animation


Planes in 3D

• Or equivalently by:
– three points (m = P-R and n = Q-R)

R Q

( β) = R + α(P-R)
P(α, (P R) + β(Q-R)
β(Q R)

CSCI 3161- Computer Animation


Normals

• Every plane has a vector normal n (perpendicular) to it


• we can use the cross product to find n

CSCI 3161- Computer Animation


The Cross Product

• To compute the cross product between 3D vectors


u and v:
u × v = ( uyvz − uz vy , uz vx − uxvz , uxvy − uyvx )

CSCI 3161- Computer Animation


Questions

CSCI 3161- Computer Animation

S-ar putea să vă placă și