1
$\begingroup$

This question already has an answer here:

How can I connect given coordinates and create figure from it? Can I fill it with colour? I tried:

Line[{a[[1]], a[[2]], a[[3]], a[[4]], a[[5]], a[[1]]}]

where a is my list. It doesnt look good.

a consists of coordinates in 2D {x,y} pairs. I want to connect all points to form a region/space/figure and fill that region with colour.

$\endgroup$

marked as duplicate by Yves Klett, Öskå, MarcoB, user9660, Jason B. Jan 18 '16 at 9:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ We need more information. Provide an example list a. What do you mean by "fill it with color"? - the line has a color? - color fill between the line and the axis? What do you mean by "it doesn't look good"? Did you embed the Line in a Graphics object? Etc. $\endgroup$ – march Dec 17 '15 at 19:45
  • $\begingroup$ a consists of coordinates {{-0.9504, -0.31103}, {-0.946885, -0.321572}, {0.848087, 0.529857}, {0.998268, -0.0588254}} I want to connect them to form a region/space/figure and fill that region with colour. $\endgroup$ – Tsin Dec 17 '15 at 19:48
  • $\begingroup$ Please add relevant information to your post by clicking the grey edit button at the bottom of the post. In addition, please format your code correctly. $\endgroup$ – march Dec 17 '15 at 19:50
  • $\begingroup$ Done. Can you answer my question please? $\endgroup$ – Tsin Dec 17 '15 at 19:54
  • 5
3
$\begingroup$

Just for fun

(*psuedo-data*)
data = RandomReal[{0, 10}, {5, 2}];

(*process*)
Manipulate[
  Graphics[{col, Polygon @ pts}],
  {{pts, data}, Locator},
  {{col, Green, "Color"}, {Green, Blue, Red}}]

polygon

$\endgroup$
  • $\begingroup$ Omg 1k reputation! $\endgroup$ – e.doroskevic Jan 17 '16 at 16:03
1
$\begingroup$

To avoid polygons whose edges cross themselves, try this:

ConvexHullMesh[a]

For instance:

a = RandomReal[{0, 1}, {5, 2}];

Graphics[Polygon[a]]

enter image description here

ConvexHullMesh[a]

enter image description here

or

ConvexHullMesh[a, PlotTheme -> "Lines", MeshCellStyle -> Black]

enter image description here

Note that FindShortestTour does not work:

a = {{0, 0}, {1, 0}, {1, .1}, {2, .1}, {2, 0}, {3, 0}, {1.5, 1}};

Graphics[Line[a[[FindShortestTour[a][[2]]]]]]

enter image description here

which is not convex.

$\endgroup$
  • 1
    $\begingroup$ Why is it a problem if the FindShortestTour solution is not convex? $\endgroup$ – Rahul Dec 18 '15 at 1:22
  • $\begingroup$ There is no reason the shortest tour need be convex... The shortest tour must go through every point, which includes those in the interior of the convex hull. Hence the shortest tour will rarely be convex. $\endgroup$ – David G. Stork Dec 18 '15 at 1:25
  • $\begingroup$ Yes, that's fine, but why is that a problem? You say this approach "does not work", but the asker didn't ask for a convex figure. $\endgroup$ – Rahul Dec 18 '15 at 1:28
  • $\begingroup$ Yes he did... when he wrote: "it doesn't always work. For some values sometimes it creates figures like this i.imgur.com/tAeOCSa.png?1 I want it to create geometric figures." $\endgroup$ – David G. Stork Dec 18 '15 at 1:29
  • $\begingroup$ @David that's self intersection, but non convex is probably fine. $\endgroup$ – LLlAMnYP Jan 17 '16 at 1:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.