2

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)

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))

enter image description here

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

enter image description here

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))

My question

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?

0

3 Answers 3

10

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))),])

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

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

Sign up to request clarification or add additional context in comments.

Comments

9

Here is one possibility. The idea is to use the angle around the center for ordering:

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)

xnew <- x[order(Arg(scale(x) + scale(y) * 1i))]
ynew <- y[order(Arg(scale(x) + scale(y) * 1i))]

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

resulting plot

1 Comment

@Pop It's your responsibility to provide a representative example. However, the basic idea of using the angle around the center can be extended to higher dimensions.
1

Just use the geometry package with the function convhulln

Here the example they provide (see ?convhulln)

ps <- matrix(rnorm(3000), ncol=3) # generate points on a sphere
ps <- sqrt(3)*ps/drop(sqrt((ps^2) %*% rep(1, 3)))
ts.surf <- t(convhulln(ps)) # see the qhull documentations for the options
rgl.triangles(ps[ts.surf,1],ps[ts.surf,2],ps[ts.surf,3],col="blue",alpha=.2)

For plotting you need the rgl-package

Result: enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.