How to check if a polygon is completely inside another polygon

本文介绍了一个基于PolygonClipperAS3类的简单解决方案,用于判断一个凸包是否完全包含于另一个凸包内。通过使用此方法,可以轻松实现并观察结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Checking if a polygon is completely inside another polygon is a very common problem in geometry, and most solutions you can find in the web aren’t that easy, especially when you are dealing with irregular polygons.

Today I want to show you a quick solution based on the PolygonClipper AS3 class.

Basically we can say a polygon called A is completely inside another polygon called B when the intersection between these two polygons returns the same polygon A.

So, mixing the concepts seen in understanding polygon clipping and introducing PolygonClipper AS3 class and AS3 code snippet: draw a star and determine its area I was able to do this prototype in a few minutes:




Use the mouse to move the small star and watch what happens when it’s inside the big star.

This is the commented source code:

package {
import flash.display.Sprite;
import flash.geom.Point;
import flash.events.Event;
import flash.text.TextField;
import com.logicom.geom.Clipper;
import com.logicom.geom.ClipType;
public class Main extends Sprite {
private var mainPolygon:Array;
private var clipPolygon:Array;
private var clipCanvas:Sprite=new Sprite();
private var txt:TextField=new TextField();
public function Main():void {
var mainCanvas:Sprite=new Sprite();
addChild(mainCanvas);
// this is the main polygon
mainPolygon=createStar(7,new Point(320,240),220,120,180);
drawPolygon(mainPolygon,mainCanvas,0x0000FF);
addChild(clipCanvas);
addChild(txt);
txt.width=200;
addEventListener(Event.ENTER_FRAME,clip);
}
private function clip(e:Event):void {
// clearing the canvas of the moving polygon
clipCanvas.graphics.clear();
// this is the polygon we want to check if it's completely inside the main polygon
var clipPolygon:Array=createStar(5,new Point(mouseX,mouseY),100,70,0);
// calculating the area of the moving polygon
var clipArea:Number=getArea(clipPolygon);
// drawing the moving polygon, using a red color
drawPolygon(clipPolygon,clipCanvas,0xFF0000);
// getting the polygons forming the intersection between the main polygon and the moving polygon
var resultPolygons:Array=Clipper.clipPolygon(mainPolygon,clipPolygon,ClipType.INTERSECTION);
// in totalArea variable we will sum the areas of all polygons (if more than one) forming the intersection
// between the main polygon and the  moving polygon
var totalArea:Number=0;
// looping through all the polygons forming the intersection
for (var i:int=0; i<=resultPolygons.length-1; i++) {
// drawing the intersection polygon (that is the polygon inside the main polygon) with a green background
drawPolygon(resultPolygons[i],clipCanvas,0x00FF00);
// updating total area
totalArea+=getArea(resultPolygons[i]);
}
// if the total intersection area is equal to the area of the moving polygon, we can say
// the moving polygon is completely inside the main polygon
if (totalArea==clipArea) {
txt.text="COMPLETELY INSIDE";
}
else {
// otherwise, let's show the ratio of the moving polygon inside the main polygon
txt.text=Math.floor(totalArea/clipArea*100).toString()+"%";
}
}
// simple function to draw a polygon given an array with vertices, a display object where to draw it and a fill color
private function drawPolygon(polygon:Array,canvas:Sprite,color:Number):void {
canvas.graphics.lineStyle(3,0x000000,1);
canvas.graphics.beginFill(color,1);
var n:uint=polygon.length;
if (n<3) {
return;
}
var p:Point=polygon[0];
canvas.graphics.moveTo(p.x, p.y);
for (var i:uint = 1; i <= n; ++i) {
p=polygon[i%n];
canvas.graphics.lineTo(p.x, p.y);
}
canvas.graphics.endFill();
}
// function to draw a star. More information at 
// http://www.emanueleferonato.com/2013/06/22/as3-code-snippet-draw-a-star-and-determine-its-area/
private function createStar(arms:int,center:Point,outer:Number,inner:Number,offsetAngle:Number):Array {
offsetAngle*=0.0174532925;
var starArray:Array=new Array();
var r:Number;
var angle:Number=Math.PI/arms;
for (var i:int=0; i<2*arms; i++) {
if ((i%2)==0) {
r=outer;
}
else {
r=inner;
}
starArray.push(new Point(Math.round(center.x+Math.cos(i*angle-angle/2+offsetAngle)*r),Math.round(center.y+Math.sin(i*angle-angle/2+offsetAngle)*r)));
}
return starArray;
}
// function to get the area of any polygon
private function getArea(poly:Array):Number {
var i:int=0;
var j:int=0;
var n:int=poly.length;
var surface:Number=0;
for (i=0; i<n; i++) {
j=(i+1)%n;
surface+=Point(poly[i]).x*Point(poly[j]).y;
surface-=Point(poly[i]).y*Point(poly[j]).x;
}
surface=surface/2;
return surface;
}
}
}


Checking if a polygon is inside another polygon is the last theoretical step before creating real physics destructible terrain, so download the source code and wait for the final prototype.


From:http://www.emanueleferonato.com/2013/07/03/how-to-check-if-a-polygon-is-completely-inside-another-polygon/

### 将XML格式的多边形数据转换为LabelMe JSON格式 对于对象检测或图像标注的任务,将XML格式中的多边形数据转换成LabelMe使用的JSON格式是一项常见的需求。此过程涉及解析原始XML文件并提取必要的几何形状信息和标签名称。 #### 解析XML文件 通常情况下,XML文件会按照特定结构存储每个物体的位置信息和其他属性。如果这些位置是以多边形形式给出,则意味着存在一系列(x,y)坐标点来定义边界[^1]。 ```python import xml.etree.ElementTree as ET def parse_xml(xml_file): tree = ET.parse(xml_file) root = tree.getroot() shapes = [] for obj in root.findall('object'): label = obj.find('name').text bndbox = obj.find('bndbox') cx = float(bndbox.find('cx').text) cy = float(bndbox.find('cy').text) w = float(bndbox.find('w').text) h = float(bndbox.find('h').text) angle = float(bndbox.find('angle').text) points = calculate_polygon_points(cx, cy, w, h, angle) shape = { 'label': label, 'points': points, 'shape_type': 'polygon' } shapes.append(shape) return {'shapes': shapes} def calculate_polygon_points(cx, cy, width, height, rotation_angle): import math # 计算矩形四个角相对于中心点的角度偏移量 angles = [0, 90, 180, 270] radians_per_degree = math.pi / 180.0 result = [] for a in angles: radian_offset = (a + rotation_angle) * radians_per_degree dx = (width/2)*math.cos(radian_offset) dy = -(height/2)*math.sin(radian_offset) px = int(round(dx)) + cx py = int(round(dy)) + cy result.append([px,py]) return result ``` 上述代码片段展示了如何读取XML文件并将其中的对象描述转化为适合LabelMe工具处理的形式。这里假设输入的框采用的是`[cx,cy,w,h,angle]`这种表示法。 #### 创建对应的JSON文件 一旦获得了所有需要的信息之后,就可以创建一个新的JSON文件用于保存转换后的数据: ```json { "version": "4.5.6", "flags": {}, "shapes": [ {"label":"example_label","points":[[x1,y1],[x2,y2],...],"group_id":null,"shape_type":"polygon","flags":{}} ], "imagePath": "path_to_image.jpg", "imageData": null, "imageHeight": 720, "imageWidth": 1280 } ``` 注意,在实际应用中还需要设置正确的版本号、图片路径以及其他元数据字段以匹配具体的项目要求[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值