PolyPolygon, Polyline, Polygon的简单用法

BOOL Polygon(LPPOINT lpPoints, int nCount);

void CExoView::OnDraw(CDC* pDC)
{
	CPoint Pt[7];
	Pt[0] = CPoint(20, 50);
	Pt[1] = CPoint(180, 50);
	Pt[2] = CPoint(180, 20);
	Pt[3] = CPoint(230, 70);
	Pt[4] = CPoint(180, 120);
	Pt[5] = CPoint(180, 90);
	Pt[6] = CPoint(20, 90);

	pDC->Polygon(Pt, 7);
}



Drawing Polygons

void CView1View::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	CPoint PtLine[] = { CPoint( 50,  50), CPoint(670,  50),
			  CPoint(670, 310), CPoint(490, 310),
			  CPoint(490, 390), CPoint(220, 390),
			  CPoint(220, 310), CPoint( 50, 310), 
			  CPoint( 50,  50) };

	CPoint Bedroom1[] = { CPoint( 55,  55), CPoint(175,  55),
			    CPoint(175, 145), CPoint( 55, 145)};
	CPoint Closets[]  = { CPoint( 55, 150), CPoint(145, 150),
			    CPoint(145, 205), CPoint( 55, 205) };
	CPoint Bedroom2[] = { CPoint(55, 210), CPoint(160, 210),
			    CPoint(160, 305), CPoint(55, 305) };

	dc.MoveTo(PtLine[0]);
	dc.LineTo(PtLine[1]);
	dc.LineTo(PtLine[2]);
	dc.LineTo(PtLine[3]);
	dc.LineTo(PtLine[4]);
	dc.LineTo(PtLine[5]);
	dc.LineTo(PtLine[6]);
	dc.LineTo(PtLine[7]);
	dc.LineTo(PtLine[8]);

	dc.Polygon(Bedroom1, 4);
	dc.Polygon(Closets, 4);
	dc.Polygon(Bedroom2, 4);
	// Do not call CView::OnPaint() for painting messages
}

BOOL PolyPolygon(LPPOINTlpPoints, LPINT lpPolyCounts, int nCount);

void CExoView::OnDraw(CDC* pDC)
{
	CPoint Pt[12];
	int lpPts[] = { 3, 3, 3, 3 };

	// Top Triangle
	Pt[0] = CPoint(125,  10);
	Pt[1] = CPoint( 95,  70);
	Pt[2] = CPoint(155,  70);

	// Left Triangle
	Pt[3] = CPoint( 80,  80);
	Pt[4] = CPoint( 20, 110);
	Pt[5] = CPoint( 80, 140);

	// Bottom Triangle
	Pt[6] = CPoint( 95, 155);
	Pt[7] = CPoint(125, 215);
	Pt[8] = CPoint(155, 155);
	
	// Right Triangle
	Pt[9] = CPoint(170,  80);
	Pt[10] = CPoint(170, 140);
	Pt[11] = CPoint(230, 110);

	pDC->PolyPolygon(Pt, lpPts, 4);
}




### 高德地图 Polyline 转换 Polygon 并处理重叠区域 在高德地图中,Polyline 表示一条由多个顶点连接而成的线段路径,而 Polygon 则表示一个多边形闭合区域。要将 Polyline 转换成 Polygon,并解决可能存在的重叠问题,可以按照如下方法进行: #### 创建 PolylinePolygon 对象 首先定义一个函数用于创建 Polyline 或者 Polygon 的实例对象。 ```javascript function createShape(path, isPolygon) { const options = { path: path, strokeColor: "#FF0000", strokeWeight: 6, strokeOpacity: 0.8 }; if (isPolygon) { return new AMap.Polygon({ ...options, fillColor: '#F5DEB3', fillOpacity: 0.35 }); } else { return new AMap.Polyline(options); } } ``` #### 将 Polyline 路径转成 Polygon 形状 为了把 Polyline 变成 Polygon,需要确保起点等于终点形成封闭图形。这里假设输入的是已经按顺序排列好的节点数组 `path`。 ```javascript function polylineToPolygon(polylinePath) { let closedPath = [...polylinePath]; // 如果首尾不相接,则补上最后一个点作为结束点使它成为一个闭环 if (!AMap.GeometryUtil.isRing(closedPath)) { closedPath.push(closedPath[0]); } return createShape(closedPath, true); } ``` 此部分逻辑利用了 `AMap.GeometryUtil.isRing()` 来判断给定路径是否构成环形结构[^1]。 #### 解决多边形之间的重叠冲突 当存在两个或更多相互交叉或多边形时,可能会遇到面积计算错误等问题。此时应考虑使用空间几何算法库 Turf.js 进行拓扑关系分析并调整边界以消除重叠现象。 ```javascript // 假设有两个 polygon 实例 p1,p2... const polygons = [p1.getPath(), p2.getPath()]; for(let i=0; i<polygons.length-1;i++){ for(let j=i+1;j<polygons.length;j++){ var diffResult = turf.difference( turf.polygon([polygons[i]]), turf.polygon([polygons[j]]) ); // 更新原始polygon的数据源为去除交集后的结果 polygons[i]=diffResult.geometry.coordinates; } } // 使用更新后的坐标重新绘制无重叠的多边形 polygons.forEach((coords)=>{ map.add(createShape(coords,true)); }); ``` 上述代码片段展示了如何借助第三方地理信息系统(GIS)工具包——Turf.js来执行差异运算(`difference`)从而有效排除两两之间发生的重叠情况[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hellokandy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值