在R中绘制多边形 [英] Plot polygon in R

查看:197
本文介绍了在R中绘制多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从坐标为的点样本(实际上,多边形是凸包)中绘制一个多边形

I want to plot a polygon from a sample of points (in practice, the polygon is a convex hull) whose coordinates are

x <- c(0.66, 0.26, 0.90, 0.06, 0.94, 0.37)
y <- c(0.99, 0.20, 0.38, 0.77, 0.71, 0.17)

当我应用polygon函数时,得到以下图:

When I apply the polygon function I get the following plot:

plot(x,y,type="n")
polygon(x,y)
text(x,y,1:length(x))

但这不是我期望的...我想要的是以下情节:

But it is not what I expect... What I want is the following plot:

我通过执行以下操作获得了最后一个情节:

I obtained this last plot by doing:

good.order <- c(1,5,3,6,2,4)
plot(x,y,type="n")
polygon(x[good.order], y[good.order])
text(x,y,1:length(x))

我的问题

基本上,我的问题是:如何获取索引向量(在上面的代码中称为good order) 哪个可以得到我想要的多边形?

Basically, my question is: how to obtain the vector of indices (called good order in the code above) which will allow to get the polygon I want?

推荐答案

假定凸多边形,只需获取一个中心点并计算角度,然后按递增的角度排序.

Assuming a convex polygon, just take a central point and compute the angle, then order in increasing angle.

> pts = cbind(x,y)
> polygon(pts[order(atan2(x-mean(x),y-mean(y))),])

请注意,您的good.order的任何周期都可以使用,

Note that any cycle of your good.order will work, mine gives:

> order(atan2(x-mean(x),y-mean(y)))
[1] 6 2 4 1 5 3

可能是因为我在atan2中混合了xy,因此它对它的旋转角度为90度,就像这里一样重要.

probably because I've mixed x and y in atan2 and so its thinking about it rotated by 90 degrees, like that matters here.

这篇关于在R中绘制多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆