Download Computational Geometry: Numbers & Structures for Intersecting Objects - Prof. Juan Cebral and more Study notes Computer Science in PDF only on Docsity!
Data Structures and Algorithms
More geometric algorithms
Searching and sorting
Representing numbers on a computer
Computational geometry
Representation of points
Representation of segments
Representation of polygons
Representation of surfaces
- List of triangles / quads
Representation of solids
- List of tetrahedra / hexahedra
Segment intersection in 2D
int ccw(pointT p0,pointT p1,pointT p2) {
dx1=p1.x-p0.x; dy1=p1.y-p0.y; dx2=p2.x-p0.x; dy2=p2.y-p0.y; if( dx1dy2 > dy1dx2 ) return +1; if( dx1dy2 < dy1dx2 ) return -1; if( (dx1dx2<0) || (dy1dy2<0) ) return -1; if( (dx1dx1+dy1dy1)<(dx2dx2+dy2dy2)) return +1; return 0;
} int intersect(segT s1,segT s2) {
return ((ccw(s1.p1,s1.p2,s2.p1)
*ccw(s1.p1,s1,ps,s2.p2)) <=0 )
&& ((ccw(s2.p1,s2.p2,s1.p1)
*ccw(s2.p1,s2,ps,s1.p2)) <=0 );
}
Inclusion in a polygon
Problem: given a point and a polygon represented as anarray of points, determine whether the point is inside oroutside the polygon
Approach: cast a ray from the point to infinity and countthe number of intersections with polygon segments
Number is odd: inside, number is even: outside
Account for cases when the ray lies on a point or asegment
Unstructured grids
Mesh representation
- List of points + list of elements
Derived data structures
- List of elements surrounding each point – List of points connected to each point – List of neighbor elements to each element – List of boundary elements / points
These derived data structures are many times useful fordeveloping efficient algorithms on unstructured grids
Elements surrounding points
int esup1[npoin+1]; int esup2[max_store];
ip
e
1
e
2
e
3
e
4
e
5
ip
e
1
e
2
e
3
e
4
esup1 esup
point
point
point
Elements surrounding points: usage
for(i=0;
i<npoin;
i++)
loop
over
points
(i)
nel=esup1[i+1]-esup1[i];
num
elem
surr
i
lel=&esup2[esup1[i]];
list
elem
surr
i
for(j=0;
j<np;
j++)
loop
over
surr
elem
iel=lel[j];
get
next
elem
surr
I
do_work...
Points surrounding points
int psup1[npoin+1]; int psup2[max_store];
psup1 psup
point
point
point
ip
p
1
p
2
p
3
p
4
p
4
ip
p
1
p
2
p
3
p
4
p
5
p
6
p
7
p
8
Points surrounding points: usage
Do
ipoin=1,npoin
loop
over
points
jp1=psup1(ipoin)+
first
point
surr
ipoin
jp2=psup1(ipoin+1)
last
point
surr
ipoin
do
jp=jp1,jp
loop
over
pts
surr
ipoin
jpoin=psup2(jp)
gather
point
do_work...
enddo
enddo
Elements surrounding elements
int
esuel[nnode*nelem];
init
esuel[1:nnode*nelem]=-
loop
over
elements
(ie)
loop
over
nodes
of
ie
(a)
get
b=next
node
of
ie
get
c=next
node
of
ie
loop
over
elements
surrounding
b
(je
ie)
if(je
and
ie
have
nodes
in
common)
then
store
je
as
neighbor
opposite
to
a
endif endloop endloop endloop
a
b
c
e
a
e
c
e
b
e
Searching and Sorting
Searching •
Sequential search
Binary trees
Balanced trees
Hashing
Radix searching
External searching
Sorting •
Bubble sort
Selection sort
Insertion sort
Shel
l
sort
Quicksort
Radix sort
Merge sort
Heap sort
Sequential search
Suppose we are searching for an integer i
The simplest method for searching is to insert all theelements (integers) into an array and search sequentiallyfor the given number (i)
This method uses N+1 comparisons for unsuccessfulsearches and about N/2 comparisons for successfulsearches
Binary trees
A
tree
is a non-empty collection of
vertices
and
edges
A
vertex
or
node
is a simple object that can have a name
and carry other associated information
An
edge
is a connection between two vertices
A
path
in a tree is a list of distinct vertices in which
successive vertices are connected by edges in the tree
One node in the tree is designated as
root
: there is
exactly one path between the root and each of the othernodes in the tree
Binary trees
Each node has exactly one
parent
Each node has a number of
children
Nodes with no children are called
leaves
or
terminal
nodes
Nodes with children are called
internal
nodes
Any node is the root of a
subtree
A set of trees is called a
forest
The simplest tree is a
binary
tree:
nodes have no or exactly two children
A
B
C
D
E