PDA

View Full Version : polygon clipping (csg) 2d



noeska
20-04-2008, 07:39 PM
Delphi 2005 pro
C conversion
Polygon
Clipping
CSG
2D

Hello,

2D gives more chalenges then i expected.
Now i am trying to merge 2 polygon shapes to a new one.
Doing a search on the inet i found the following: http://davis.wpi.edu/~matt/courses/clipping/ That looks doable. But looking at the c source i get somewhat confused again as:



typedef struct _node
{
int x, y;
struct _node *next;
struct _node *prev;
struct _node *nextPoly; /* pointer to the next polygon */
struct _node *neighbor; /* the coresponding intersection point */
int intersect; /* 1 if an intersection point, 0 otherwise */
int entry; /* 1 if an entry point, 0 otherwise */
int visited; /* 1 if the node has been visited, 0 otherwise */
float alpha; /* intersection point placemet */
} node;

In delphi that could be written as:


type
tnode = class;
pnode = ^tnode;

tnode = class
x, y: single;
next: pnode;
prev: pnode;
nextPoly: pnode; // pointer to the next polygon */
neighbor : pnode; // the coresponding intersection point */
intersect: bool; // 1 if an intersection point, 0 otherwise */
entry: bool; // 1 if an entry point, 0 otherwise */
visited: bool; // 1 if the node has been visited, 0 otherwise */
alpha: single; // intersection point placemet */
end;

Now i would rather use a record. But this does not work:


tnode = record;
pnode = ^tnode;


Now this is initialized as:

node *s=0, *c=0, *root=0;
What is happening here assing 0 to record? I gues i have just to create my object and/or record here.
The following also confuses me ( i think i either adds point for polygon a or for polygon b).



void add(Widget w, int which_button, int x, int y, void *data)
{
node *new;
if (!DRAW) return;

if (which_button == 1)
{
new = malloc(sizeof(node));
new->x = x;
new->y = y;
new->prev = 0; /* not need to initialize with 0 after malloc ... */
new->nextPoly = 0;
new->neighbor = 0;
new->intersect = 0;
new->entry = 0;
new->visited = 0;
new->alpha = 0.;
if (DRAW == 1)
{
new->next = s;
if (s) s->prev = new;
s = new;
}
else /* DRAW == 2 */
{
new->next = c;
if (c) c->prev = new;
c = new;
}
redisplay(W[3], X, Y, NULL);
}
else if (which_button == 3)
{
DRAW = DRAW==1 ? 2:0;
redisplay(W[3], X, Y, NULL);
}
}
What is happinging here:


if (DRAW == 1)
{
new->next = s;
if (s) s->prev = new;
s = new;
}
the s that was created empty before is assigned to next.
now the prev of s is compared to new that is the point that is currently added? Or is s compared to prev of s and if that is the case it is made the new value? aarghh....
finaly the new is made the new s. I am getting confused here.
Thank for your help in advance.

noeska
20-04-2008, 08:10 PM
for the time being i wrote it like this:


type
pnode = ^tnode;
tnode = record
x, y: single;
next: pnode;
prev: pnode;
nextPoly: pnode; // pointer to the next polygon */
neighbor : pnode; // the coresponding intersection point */
intersect: bool; // 1 if an intersection point, 0 otherwise */
entry: bool; // 1 if an entry point, 0 otherwise */
visited: bool; // 1 if the node has been visited, 0 otherwise */
alpha: single; // intersection point placemet */
end;

var
s: pnode;

procedure Add(x,y: single);
var
newnode : pnode;
begin
new(newnode);
newnode.x := x;
newnode.y := y;
newnode.prev := nil; // not need to initialize with 0 after malloc ...
newnode.nextPoly := nil;
newnode.neighbor := nil;
newnode.intersect := false;
newnode.entry := false;
newnode.visited := false;
newnode.alpha := 0.0;

newnode.next := s;
if &#40;s<>nil&#41; then
s.prev &#58;= newnode;
s &#58;= newnode;
end;
Only to me it seems that prev and next should be named the oposite. e.g. prev should be next and next should be prev?