SlideShare a Scribd company logo
1 of 18
Download to read offline
CONVEX PARTITIONING OF A POLYGON INTO MINIMIZED NUMBER OF
PIECES
K. R. Wijeweera, S. R. Kodituwakku
Paper:
https://www.academia.edu/32022683/Convex_Partitioning_of_a_Polygon_into_Smaller_Numbe
r_of_Pieces_with_Lowest_Memory_Consumption
APPENDIX
This section includes the implementation of the proposed algorithm in C programming language.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include"D:/header/grap.h"
double x[]={40,50,80,130,140,150,250,190,140,90,80,30,20,50,20,40,10};
double y[]={10,40,10,20,70,20,90,130,80,70,130,180,140,120,100,60,50};
int points=17;
int reflex[20];
int secs=0;
double sx[20];
double sy[20];
double ex[20];
double ey[20];
double xn,yn,xg,yg;
int found;
int dpx(double x)
{
int p;
p=(int)(x+0.5);
return p;
}
int dpy(double y)
{
int p;
p=(int)(y+0.5);
p=getmaxy()-p;
return p;
}
void drawPolygon()
{
int i,j;
for(i=0;i<points;i++)
{
j=(i+1)%points;
line(dpx(x[i]),dpy(y[i]),dpx(x[j]),dpy(y[j]));
}
}
double t(double x1,double y1,double x2,double y2,double x3,double y3)
{
return x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2);
}
int tv(double x1,double y1,double x2,double y2,double x3,double y3)
{
if(t(x1,y1,x2,y2,x3,y3)>0)
{
return 1;
}
else if(t(x1,y1,x2,y2,x3,y3)<0)
{
return -1;
}
return 0;
}
int lv()
{
int i,min=0;
double miny=y[0];
for(i=1;i<points;i++)
{
if(miny>y[i])
{
min=i;
miny=y[i];
}
}
return min;
}
int getWise(int v)
{
int a=v-1,b=v+1;
if(a==-1)
{
a=points-1;
}
if(b==points)
{
b=0;
}
return tv(x[a],y[a],x[v],y[v],x[b],y[b]);
}
void setReflex()
{
int min,i;
min=lv();
for(i=0;i<points;i++)
{
if(getWise(i)*getWise(min)<0)
{
reflex[i]=1;
}
}
}
int isTwoSides(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double
y4)
{
if(tv(x1,y1,x2,y2,x3,y3)*tv(x1,y1,x2,y2,x4,y4)<0)
{
return 1;
}
return 0;
}
int isIntersect(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double
y4)
{
if(isTwoSides(x1,y1,x2,y2,x3,y3,x4,y4)*isTwoSides(x3,y3,x4,y4,x1,y1,x2,y2))
{
return 1;
}
return 0;
}
int pos(int k,double yc)
{
if(y[k]>yc)
{
return 1;
}
else if(y[k]<yc)
{
return -1;
}
return 0;
}
int isInside(double xc,double yc)
{
int i,j;
int p=0;
double xp;
int a,b;
for(i=0;i<points;i++)
{
j=(i+1)%points;
if(pos(i,yc)*pos(j,yc)<0)
{
xp=x[i]+(yc-y[i])*(x[j]-x[i])/(y[j]-y[i]);
if(xp>xc)
{
p++;
}
}
if((y[i]==yc) && (x[i]>xc))
{
a=i-1;
b=i+1;
if(a==-1)
{
a=points-1;
}
if(b==points)
{
b=0;
}
if(pos(a,yc)*pos(b,yc)<0)
{
p++;
}
if((y[j]==yc) && (x[j]>xc))
{
a=i-1;
b=j+1;
if(a==-1)
{
a=points-1;
}
if(b==points)
{
b=0;
}
if(pos(a,yc)*pos(b,yc)<0)
{
p++;
}
}
}
}
if((p%2)==0)
{
return 0;
}
return 1;
}
int isUseful(int i,int j)
{
int a,b,t=0;
a=(i-1)%points;
b=(i+1)%points;
if(tv(x[i],y[i],x[j],y[j],x[a],y[a])*tv(x[i],y[i],x[j],y[j],x[b],y[b])>0)
{
t=1;
}
a=(j-1)%points;
b=(j+1)%points;
if(tv(x[i],y[i],x[j],y[j],x[a],y[a])*tv(x[i],y[i],x[j],y[j],x[b],y[b])>0)
{
t=1;
}
if(t==0)
{
return 1;
}
return 0;
}
void setSectors1()
{
int i,j,k,p,found;
double midx,midy;
for(i=0;i<points-1;i++)
{
for(j=i+1;j<points;j++)
{
if(reflex[i]*reflex[j]==0)
{
continue;
}
if(j-i>1)
{
found=0;
for(k=0;k<points;k++)
{
p=(k+1)%points;
if(isIntersect(x[i],y[i],x[j],y[j],x[k],y[k],x[p],y[p]))
{
found=1;
}
}
if(found==0)
{
for(k=0;k<points;k++)
{
if(tv(x[i],y[i],x[j],y[j],x[k],y[k])==0)
{
if(x[i]==x[j])
{
if(y[i]<y[j])
{
if((y[k]>y[i]) &&
(y[k]<y[j]))
{
found=1;
}
}
else
{
if((y[k]<y[i]) &&
(y[k]>y[j]))
{
found=1;
}
}
}
else
{
if(x[i]<x[j])
{
if((x[k]>x[i]) &&
(x[k]<x[j]))
{
found=1;
}
}
else
{
if((x[k]<x[i]) &&
(x[k]>x[j]))
{
found=1;
}
}
}
}
}
if(found==0)
{
midx=(x[i]+x[j])/2;
midy=(y[i]+y[j])/2;
if(isInside(midx,midy))
{
if(isUseful(i,j))
{
reflex[i]=0;
reflex[j]=0;
sx[secs]=x[i];
sy[secs]=y[i];
ex[secs]=x[j];
ey[secs]=y[j];
secs++;
}
}
}
}
}
}
}
}
void drawSectors()
{
int i;
for(i=0;i<secs;i++)
{
line(dpx(sx[i]),dpy(sy[i]),dpx(ex[i]),dpy(ey[i]));
}
}
void findIntersection(double x1,double y1,double x2,double y2,double x3,double y3,double
x4,double y4)
{
double m1,c1,m2,c2;
if(x1==x2)
{
m2=(y3-y4)/(x3-x4);
c2=y3-m2*x3;
xn=x1;
yn=m2*xn+c2;
}
else
{
m1=(y1-y2)/(x1-x2);
c1=y1-m1*x1;
if(x3==x4)
{
xn=x3;
yn=m1*xn+c1;
}
else
{
m2=(y3-y4)/(x3-x4);
c2=y3-m2*x3;
xn=(c2-c1)/(m1-m2);
yn=m1*xn+c1;
}
}
}
double ab(double x)
{
if(x<0)
{
return -x;
}
return x;
}
void setClosest(int i,int j)
{
double dx,dy;
dx=x[i]-x[j];
dy=y[i]-y[j];
if(ab(dx)>ab(dy))
{
if(dx*(xn-x[j])<0)
{
if(found==0)
{
xg=xn;
yg=yn;
found=1;
}
else
{
if(dx>0)
{
if(xn>xg)
{
xg=xn;
yg=yn;
}
}
else
{
if(xn<xg)
{
xg=xn;
yg=yn;
}
}
}
}
}
else
{
if(dy*(yn-y[j])<0)
{
if(found==0)
{
xg=xn;
yg=yn;
found=1;
}
else
{
if(dy>0)
{
if(yn>yg)
{
xg=xn;
yg=yn;
}
}
else
{
if(yn<yg)
{
xg=xn;
yg=yn;
}
}
}
}
}
}
void setSectors2()
{
int i,j,p,q;
for(i=0;i<points;i++)
{
j=(i+1)%points;
if(reflex[j]==1)
{
found=0;
for(p=0;p<points;p++)
{
q=(p+1)%points;
if(isTwoSides(x[i],y[i],x[j],y[j],x[p],y[p],x[q],y[q]))
{
findIntersection(x[i],y[i],x[j],y[j],x[p],y[p],x[q],y[q]);
setClosest(i,j);
}
}
for(p=0;p<points;p++)
{
if(t(x[i],y[i],x[j],y[j],x[p],y[p])==0)
{
xn=x[p];
yn=y[p];
setClosest(i,j);
}
}
for(p=0;p<secs;p++)
{
if(isTwoSides(x[i],y[i],x[j],y[j],sx[p],sy[p],ex[p],ey[p]))
{
findIntersection(x[i],y[i],x[j],y[j],sx[p],sy[p],ex[p],ey[p]);
setClosest(i,j);
}
}
for(p=0;p<secs;p++)
{
if(t(x[i],y[i],x[j],y[j],sx[p],sy[p])==0)
{
xn=sx[p];
yn=sy[p];
setClosest(i,j);
}
}
for(p=0;p<secs;p++)
{
if(t(x[i],y[i],x[j],y[j],ex[p],ey[p])==0)
{
xn=ex[p];
yn=ey[p];
setClosest(i,j);
}
}
sx[secs]=x[j];
sy[secs]=y[j];
ex[secs]=xg;
ey[secs]=yg;
secs++;
}
}
}
void main()
{
ginit();
setcolor(15);
drawPolygon();
setReflex();
setSectors1();
setSectors2();
setcolor(10);
drawSectors();
getch();
gexit();
}
Following is the set of polygons used for comparing with the Hertel Mehlhorn algorithm.
Comparison results are shown in Table 1 and Table2.
// 1
double
x[]={165,15,45,60,90,15,60,15,150,60,150,90,210,270,405,270,195,315,255,330,480,420,525,48
0,435,405,555,345};
double
y[]={15,45,135,75,165,135,240,315,240,195,165,105,165,285,270,225,90,165,150,225,180,300,
255,150,180,150,75,150};
int points=28;
// 2
double x[]={210,255,345,420,465,495,420,420,180,330,90,135,180,90,120,45,120,30};
double y[]={30,180,225,180,255,255,315,285,345,255,255,270,330,285,345,285,225,165};
int points=18;
// 3
double
x[]={210,450,390,300,360,270,270,360,285,225,285,240,195,90,120,15,60,120,90,45,45,15,120,
150,150};
double
y[]={90,195,240,195,180,180,195,255,360,300,315,180,300,240,315,225,120,195,90,90,45,15,6
0,30,225};
int points=25;
// 4
double
x[]={300,345,465,540,600,495,390,435,390,285,345,390,285,300,135,195,75,30,135,180,330,19
5,270,270,435};
double
y[]={45,15,60,195,225,270,210,195,180,300,210,165,165,255,315,195,135,30,90,30,135,105,24
0,150,150};
int points=25;
// 5
double x[]={255,480,480,600,600,480,390,330,210,120,105,255,60,15,90,15,120,165,90,210};
double y[]={60,165,120,120,240,195,285,150,240,165,225,300,345,270,165,45,15,105,90,210};
int points=20;
// 6
double x[]={180,270,435,555,405,465,300,390,240,45,255,330,195,240,90,210,105,165,30};
double y[]={15,135,45,165,270,165,165,225,330,210,270,210,165,225,180,135,75,45,45};
int points=19;
// 7
double x[]={75,330,510,300,375,270,390,300,150,300,75,240,60,30,120};
double y[]={30,30,210,345,195,150,150,75,120,210,210,300,315,30,195};
int points=15;
// 8
double x[]={225,405,540,420,300,480,225,75,300,240,195,195,270,210,150,45,90,45,225};
double y[]={45,90,15,150,90,315,360,195,285,210,225,150,180,105,180,165,90,30,75};
int points=19;
// 9
double x[]={330,510,570,495,510,420,465,360,210,300,135,225,120,30,240,345,225,345,420};
double y[]={45,15,225,165,120,150,180,330,360,285,240,60,135,45,15,135,105,285,90};
int points=19;
// 10
double x[]={345,450,510,345,405,210,360,255,270,225,285,90,165,240,30,30,180,150};
double y[]={105,30,240,165,315,270,120,180,195,180,105,270,135,90,90,15,75,15};
int points=18;
// 11
double x[]={330,465,345,345,405,405,240,330,210,135,270,330,330,195,225,150,75,45};
double y[]={30,225,330,180,240,165,165,270,330,195,90,135,60,90,105,150,105,30};
int points=18;
// 12
double x[]={225,435,570,435,435,345,450,240,255,90,180,45,45,30,30,225,255,315};
double y[]={30,90,45,270,120,225,330,360,225,150,120,120,315,315,15,60,195,120};
int points=18;
// 13
double x[]={210,405,270,375,510,450,330,390,270,180,30,210,105,300,120,30,270};
double y[]={30,30,60,135,15,210,135,300,150,330,270,210,180,120,135,15,90};
int points=17;
// 14
double
x[]={450,600,465,315,315,255,195,225,255,300,180,180,45,60,135,45,255,420,510,420,450,375
};
double
y[]={30,105,255,255,120,240,120,120,180,105,60,210,210,150,165,75,15,225,90,120,150,150};
int points=22;
// 15
double
x[]={210,375,510,360,495,330,345,225,285,225,285,315,285,195,345,210,165,45,45,165,105,21
0,165,45};
double
y[]={15,60,195,135,270,225,90,45,150,150,180,165,225,225,285,315,225,315,195,120,225,195,
60,30};
int points=24;
// 16
double
x[]={360,525,360,435,435,540,465,525,480,390,240,315,210,165,225,180,90,165,105,60,135,30
,135,300};
double
y[]={15,45,75,150,120,180,180,240,315,120,210,240,345,255,270,105,105,195,315,165,210,30,
30,150};
int points=24;
// 17
double
x[]={390,585,495,420,345,345,465,360,495,495,360,255,300,180,90,210,270,180,195,90,90,45,
225};
double
y[]={30,105,285,285,345,210,255,135,180,105,75,255,330,330,195,240,210,135,195,165,75,30,
105};
int points=23;
// 18
double
x[]={285,390,435,570,435,465,345,270,405,510,420,450,300,240,105,30,165,240,225,405};
double y[]={60,75,15,165,315,210,300,210,210,165,105,165,150,225,165,30,30,150,90,135};
int points=20;
// 19
double x[]={360,495,615,525,405,450,300,360,225,135,30,105,285,240};
double y[]={15,75,30,210,120,285,330,195,150,225,120,30,105,45};
int points=14;
// 20
double x[]={330,330,180,225,390,360,540,420,225,90,90,30,120,30,165};
double y[]={30,210,165,285,210,60,165,315,315,210,315,225,165,15,135};
int points=15;

More Related Content

More from Kasun Ranga Wijeweera

More from Kasun Ranga Wijeweera (20)

Decorator Design Pattern in C#
Decorator Design Pattern in C#Decorator Design Pattern in C#
Decorator Design Pattern in C#
 
Singleton Design Pattern in C#
Singleton Design Pattern in C#Singleton Design Pattern in C#
Singleton Design Pattern in C#
 
Introduction to Design Patterns
Introduction to Design PatternsIntroduction to Design Patterns
Introduction to Design Patterns
 
Algorithms for Convex Partitioning of a Polygon
Algorithms for Convex Partitioning of a PolygonAlgorithms for Convex Partitioning of a Polygon
Algorithms for Convex Partitioning of a Polygon
 
Geometric Transformations II
Geometric Transformations IIGeometric Transformations II
Geometric Transformations II
 
Geometric Transformations I
Geometric Transformations IGeometric Transformations I
Geometric Transformations I
 
Introduction to Polygons
Introduction to PolygonsIntroduction to Polygons
Introduction to Polygons
 
Bresenham Line Drawing Algorithm
Bresenham Line Drawing AlgorithmBresenham Line Drawing Algorithm
Bresenham Line Drawing Algorithm
 
Digital Differential Analyzer Line Drawing Algorithm
Digital Differential Analyzer Line Drawing AlgorithmDigital Differential Analyzer Line Drawing Algorithm
Digital Differential Analyzer Line Drawing Algorithm
 
Loops in Visual Basic: Exercises
Loops in Visual Basic: ExercisesLoops in Visual Basic: Exercises
Loops in Visual Basic: Exercises
 
Conditional Logic: Exercises
Conditional Logic: ExercisesConditional Logic: Exercises
Conditional Logic: Exercises
 
Getting Started with Visual Basic Programming
Getting Started with Visual Basic ProgrammingGetting Started with Visual Basic Programming
Getting Started with Visual Basic Programming
 
CheckBoxes and RadioButtons
CheckBoxes and RadioButtonsCheckBoxes and RadioButtons
CheckBoxes and RadioButtons
 
Variables in Visual Basic Programming
Variables in Visual Basic ProgrammingVariables in Visual Basic Programming
Variables in Visual Basic Programming
 
Loops in Visual Basic Programming
Loops in Visual Basic ProgrammingLoops in Visual Basic Programming
Loops in Visual Basic Programming
 
Conditional Logic in Visual Basic Programming
Conditional Logic in Visual Basic ProgrammingConditional Logic in Visual Basic Programming
Conditional Logic in Visual Basic Programming
 
Assignment for Variables
Assignment for VariablesAssignment for Variables
Assignment for Variables
 
Assignment for Factory Method Design Pattern in C# [ANSWERS]
Assignment for Factory Method Design Pattern in C# [ANSWERS]Assignment for Factory Method Design Pattern in C# [ANSWERS]
Assignment for Factory Method Design Pattern in C# [ANSWERS]
 
Assignment for Events
Assignment for EventsAssignment for Events
Assignment for Events
 
Mastering Arrays Assignment
Mastering Arrays AssignmentMastering Arrays Assignment
Mastering Arrays Assignment
 

Recently uploaded

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Recently uploaded (20)

%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 

Convex partitioning Algorithm