2

I have a matrix A that define ordered segments of a self-intersecting polygon:

A <- t(matrix(c(
         0,  0  ,
         1,  0  ,
         1, -2  ,
        -2, -2  ,
        -2, -1  ,
         0, -1  ,
         0, -4  ,
        -1, -4  ,
        -1, -2  ,
         2, -2  ,
         2, -3  ,
         0, -3  ,
         0, 0), nrow = 2));

par(mfrow=c(1,3)) 

plot(A, col='red', type= 'l', xlim=c(min(A[,1]),max(A[,1])),
      ylim=c(min(A[,2]),max(A[,2])), xlab='x', ylab='y'); 
points(A, col='black', pch = 22); 
grid()  

I heed to map the matrix A to an undirected graph where a point (x,y) corresponds to a vertex and a segment between the "neighboring" points corresponds to an edge. The neighboring points (by distance but not id's number) connected by red lines on the right fugure.

Edit. After the user20650's comment I have mapped the matrix to the undirected graph (middle graph on the figure). The undirected graph looks like an expected result. But with the edge.curved=TRUEoption (rigth fugure) we see the edges (3,4), (6,7), (9,10) and (12, 13).

library(igraph)
g <- make_empty_graph(n=nrow(A)); 
g <- g + path(seq_len(nrow(A))); 
plot(as.undirected(g), layout=as.matrix(A))
plot(g, layout=as.matrix(A), edge.curved=TRUE)

The length of edges must be equal to 1. Based on the condition we should add 5 vetries to the graph g and corresponded edges.

I can delete the edge (3,4) and add edges (3,9) and (9,4) and so on for pairs (12, 13), (9,10) and (6,7).

Question. Is there exist a way to such mapping?

enter image description here

Nick
  • 1,086
  • 7
  • 21
  • 1
    does this do what you want; `library(igraph) ; g <- make_empty_graph(n=nrow(A)) ; g <- g + path(seq_len(nrow(A))) ; plot(g, layout=as.matrix(A))` – user20650 Apr 24 '19 at 10:42
  • I have added a remark to my original question. Is it possible to add extra vertex that defines by intersections of two segments? – Nick Apr 24 '19 at 11:25
  • which vertices is the extra vertex is connected to? can you do it manually with `add_vertices`, `add_edges`? – user20650 Apr 24 '19 at 11:29
  • In your graph the extra vertex should be connected with 3, 6, 9 and 12. Of course I can add manually a new vertex. Is it possible to do automatically because edges are "crossed"? – Nick Apr 24 '19 at 11:36
  • okay. so that makes me think the code in my earlier comment is not what you want. The path from vertex 3 (1, -2) to vertex 4 (-2, -2) passes through vertex 9 (-1, -2) but there is not an edge between them. Similarly 7 and 12 etc – user20650 Apr 24 '19 at 11:52
  • @user20650, I have re-written the question and added some details. – Nick Apr 29 '19 at 05:48

0 Answers0