问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

计算机图形学的weiler-atherton算法代码

发布网友 发布时间:2022-05-12 13:27

我来回答

1个回答

热心网友 时间:2023-07-30 20:10

CS 535
WEILER-ATHERTON
PROBLEM
Implement Weiler-Atherton. You should draw each polygon in a different color and fill the clip areas generated with a third color.
NOTES:
The Weiler-Atherton algorithm is the most general of the clipping algorithms. We have two 2D polygons (may be convex or concave and they both may have holes). The polygon to be clipped is called the SUBJECT polygon and the polygon defining the clipping area is called the CLIP polygon. To make the algorithm work easier (in the data structures, etc.) we usually assume that the exterior vertices are given clockwise and the hole vertices are given counterclockwise. In clipping we usually want to find the parts of the subject polygon that are inside the clip polygon. However, this algorithm can be used in modeling to find the "union", "intersection", and "difference" of the polygons.
The data structures are several circular linked lists of vertices which are also linked together and the clipping is done by traversing these. The lists could be doubly linked. This enables the traversal in either direction at any node in the list. Starting at a particular node and traversing in one direction will proce the interior polygon(s) while starting at a different node and traversing can proce the outside. Note that procing the exterior does need the doubly linking and care must be taken in performing the traversal.

STEP 1:
The first phase of the building of the data structures occurs when we input the edges and put them in two linked lists - the SUBJ list and the CLIP list. The vertices are place in order from input (thus clockwise). There are separate lists for the exterior and for each hole. Thus, the initial build is a standard queue insert (input a vertex - insert it at end of list). Note that this creates a list in which a standard list traversal is equivalent to "walking around" the polygon edge visiting the vertices in order.
STEP 2:
The second phase of the list building is computing and inserting the INTERSECTION points. If we have a SUBJECT polygon edge (SVi to SVi+1) that intersects a CLIP polygon edge (CVj to CVj+1) at a point INTER. Note that the edges form straight lines that may intersect, we are assuming that the line segments SVi to SVi+1 intersects the line segment CVj to CVj+1. The intersection point INTER is then inserted on BOTH of the linked lists - SUBJ and CLIP. It is inserted BETWEEN SVi and SVi+1 on the SUBJ list and CVj and CVj+1 on the CLIP list. The idea is still that traversing the list using a standard list traversal we would encounter the points in their geometric order. Note that there may be several intersection points between any given pair of vertices and they must be inserted in the correct order in the lists. This is done by using the t and u values computed in the vector line intersection subprogram. Each intersection point thus has TWO nodes - one on each list (SUBJ and CLIP). We link these two entries together which provides a way of getting from one list to the other.
STEP 2.5:
Any straight line divides the plane into two half-planes. Thus each polygon edge (extended to a line) will divide the plane into two half-planes. Because we listed the vertices clockwise, we consider the half-plane to the right as containing the interior of the polygon. Thus the right half-plane is called the interior half-plane. If we consider ourselves as "walking along" a polygon edge, then we can categorize each of the INTERSECTION points as "entering" or "exiting". This is usually done from the SUBJ polygon's point of view. Thus, as we walk along the SUBJ polygon edge SVi to SVi+1 and we encounter intersection point INTER, we can ask the question - am I "entering" the CLIP polygon or am I "exiting" the CLIP polygon? The second part of computing the intersection point is to classify them as "entering" or "exiting". We create one or two lists - one for entering intersections and one for exiting intersections.
STEP3:
Once the lists are built the basic idea to do the clipping is as follows
Pick an entry from the ENTERING list - it will be an intersection point (and delete it)
Locate that point on the SUBJ list
Traverse the current (SUBJ) list until you find the next intersection point - it should be an exiting or entering point. Output each vertex encountered to some data structure, say POLY
Follow the link from the current (SUBJ) list to the other (CLIP) list and
Continue the traversal until you find the next intersection (Note: delete each entering intersection from the ENTERING list - not the linked lists. By deleting it we will get the distinct intersecting polygons and not plicate a polygon multiple times).
Terminate the traversal when you get to an intersection that is the SAME as the initial one that you removed from the ENTERING list. At this point POLY will have one of the clip regions and can be output.
REPEAT the construction and output of POLY until the ENTERING list is empty.
Remark: For the exterior, try starting with an EXITING point. Start the traversal on the SUBJ list (same direction as the Interior). At what point do you need to use the double link and to traverse in the opposite direction? (Hint: look at the CLIP polygon list).
IMPLEMENTATION:
In the below data structures we place all of the vertices and intersection points in a 1D array and use the subscript instead of the actual coordinates.
const int MAXVERT = 500;
const int MAXPOLYV = 50;
const int MAXH = 10;

struct Point2D
{float x,y;
};
typedef Point2D Vertices[MAXVERT];

enum VerType = {Polygon, Intersection};

typedef struct ClipListRec * ClipPtr;
struct ClipListRec
{ int Vindex;
ClipPtr next;
VerType Kind;
float t;
ClipPtr otherList;
}

struct Polygon
{ int nVertex;
int vert[MAXPOLYV];
ClipPtr list;
}
struct GenPolygon
{ Polygon exterior;
int numHoles;
Polygon Holes[MAXH];
}

GenPolygon Sub,Clip;
int entering[MAXVERT],exiting[MAXVERT];
Vertices V;
int numVertex = 0; // size of the array of verticies
int clipPoly[MAXVERT]; // a clip polygon

int readPoint();
{ cin >> inX; cin >> inY;
if (numVertex < MAXVERT)
{ V[numVertex].x = inX;
V[numVertex].y = inY;
idNo = numVertex;
numVertex++;
}
else
idNo = -1;
return idNo;
}

void readPolygon (GenPolygon p)
{ cin >> p.exterior.nVertex;
for (i = 0; i < p.exterior.nVertex; i++)
{ newId = readPoint();
if (newId < 0)
error
else
{ insertAtRear (p.exterior.list,newId);
p.exterior.vert[i] = newId;
}
}
// now do the holes basically the same way
. . .
}
// the "main" program loop would then be (pseudocode)
while (!EMPTY(entering))
{ nextInter = delete (entering);
SEARCH (SubjectPolygon,nextInter,ptr1);
AddToOutputList (ptr1->. . .)
StartPoint = ptr1->. . .
ptr1 = prt1->next;
while (ptr1->. . . != StartPoint)
{ AddToOutputList (ptr1->. . .);
if (ptr1-> . . == INTERSECTION)
ptr1 = prt1->otherList->next;
else
ptr1 = ptr1->next;
}
FixListForOutput();
DrawPolygon();
EmptyOutputList();
}

参考资料:http://cs1.bradley.e/public/jcm/weileratherton.html

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
请问这台电脑是什么型号的? 闲鱼跟淘宝有关联吗 袋鼠什么牌子 除了苹果还有什么减肥效果更好? 如何食用苹果醋既好喝又减肥? 苏州科技学院石湖校区有哪些学院?住宿条件好不好? 做空放贷是什么意思? 成语马前泼水是用来比喻 ...要做管理咨询吗?湖南中域康达医疗投资管理公司是湖南省卫生厅推荐的... 湖南国实控股集团有限公司集团旗下公司介绍 手机视频黑屏怎么办 为字怎么多音字组词 手机只要开视频就黑屏? 为的多音字 为的多音字是什么?为的多音字组完,组词可以组什么? 为的多音字? &quot;因为&quot;的&quot;为&quot;的多音字是什么? 为是多音字吗 喷蚊子的药,喷好,不离开房间会怎样 喷蚊子的能杀死蜜蜂不 喷蚊子的喷雾剂落下来的时候落眼里了怎么办 单行线标志和直行标志区别在哪? 直行车道和单行车道有什么区别?求图 怎么看是不是单行道 单行线和直行怎么区分? 关于驾驶汽车的一个问题:请用最通俗的语言解释一下什么是单行道,如何区分哪些道路是单行道以及行驶方向 城市单行道怎么区分?有什么标志? 如何辨别道路是不是单向行驶? 怎么看单行道 怎么辨别单行道是逆行还是顺向行驶? 15岁男生,敷蚕丝美白面膜前要敷补水面膜吗?敷完后要洗吗?敷完后还要不要涂护肤霜 爱心301的凝时保湿系列的护肤品怎么样? 蚕丝面膜哪款好用,学生用最好还带点美白补水效果的?麻烦大家用过的俏人雅水光蚕丝面膜推荐一下,谢谢? 财会22号文是财政部还是税务总局发布的 求一部电影:估计是越狱类型的。只记得一个镜头:男主角做了人头模型,用被子做了个睡觉的状态 财会【2016】22号文,土地使用税,房产税,车船税,印花税都计入税金及附加 找一个喜剧片:男的抱个被子哭,女的在旁边叼跟烟说别哭了,我会付责任的. HTML代码通用格式是什么 一个男的被被子裹住扔到楼下被枪乱打的电影 请问下印花税财会(2016)22印花税改计入税金及附加科目,是所有印花税 男生老是对你说,晚安 盖好被子,代表什么 一个男生爱跟我开玩笑说,让我给他洗碗、洗衣服,还开玩笑跟我借衣服穿、借被子用。他是什么意思啊? HTML基本格式是什么? 主人公被手铐靠在床上,然后从被子里拿出了特大号红色裤衩,是什么电影 新收入准则下企业销售不同*率的储值卡可能涉及的会计科目有哪些? html的格式 格力手机怎么设置来电语音 怎样关格力手机信息声音? 盈和软件蔬菜配送系统是自己研发的吗?技术实力怎么样? 有免费做采购菜的配送系统吗?那搭建的采购菜配送系统有些什么优势?