# Connect each point in the plot with all adjacent points

I have the following simple code. I need to connect each point in the plot with all adjacent points in vertical or horizontal line.

n = 5;
nodes = Flatten[
Table[{i (16 2.54)/n, j (16 2.54)/n}, {i, 0, n}, {j, 0, n}], 1];
dataPlot = ListPlot[nodes, PlotStyle -> PointSize -> Large];
nodelabels =
Table[Text[
Style[i + (n + 1) j + 1, 14, Bold], {0.7 + i (16 2.54)/n,
0.7 + j (16 2.54)/n}], {i, 0, n}, {j, 0, n}];
elementlabels =
Table[Text[
Style[i + (n) j + 1, 14,
Bold], {(16 2.54)/(2 n) + i (16 2.54)/n, (16 2.54)/(2 n) +
j (16 2.54)/n}], {i, 0, n - 1}, {j, 0, n - 1}];
Show[Graphics[{{Red, nodelabels}, {elementlabels}}], dataPlot,
AspectRatio -> 1, Axes -> True, ImageSize -> 600]


I have manually added some lines to show what I am looking for.

note: there is a way to do that using ListLinePlot but this method requires to overlay so many ListLinePlot. I would prefer to get solution using Vertex functions.

-
I don't get it. What should be the result in the end? – Öskå Jul 3 '14 at 17:53
This? dataPlot=ListPlot[nodes,PlotStyle->PointSize->Large,GridLines->Union/@Transpose‌​@nodes] – mfvonh Jul 3 '14 at 17:54
Or just GridGraph? – Öskå Jul 3 '14 at 17:58
@mfvonh, thanks for the answer. the grid did not appear with show? can you look at this issue? – Algohi Jul 3 '14 at 18:03
@mfvonh. ok I got it. the GridLines has to be placed in the Show function. Thanks – Algohi Jul 3 '14 at 18:08

n = 5;
g = GridGraph[{n + 1, n + 1}];
vc = SortBy[ PropertyValue[{g, #}, VertexCoordinates] & /@
VertexList@g, Last] # - # &@(16 2.54/n);
g1 = SetProperty[g, {VertexCoordinates -> vc, VertexLabels -> "Name",
ImagePadding -> 10, VertexStyle -> Red, VertexSize -> Small}];
Show[g1, PlotRange -> {{-1, 9 n}, {-1, 9 n}}, AspectRatio -> 1, Axes -> True]


-
Great. you may change the plot rang to PlotRange -> {{-1, 16 2.54 + 1}, {-1, 16 2.54 + 1}} for cases when n is larger than 5. can this answer work for grid with not equal spaces? – Algohi Jul 3 '14 at 18:30
What do you mean by " for grid with not equal spaces"? – Dr. belisarius Jul 3 '14 at 18:45
@Algohi You can modify the VertexCoordinates so.., yes? – Öskå Jul 3 '14 at 18:46
@Belisarius I mean not square grids. if the distance between nodes 1 and 2 vertical grids is not the same as that between nodes 2 and 3 or 3 and 4. some thing like this. – Algohi Jul 3 '14 at 18:51
@Algohi Easy. Just adjust the vc calculation – Dr. belisarius Jul 3 '14 at 19:08
ClearAll[ggF];
ggF[n_, m_, sc1_, sc2_, opts : OptionsPattern[Graph]] :=
GridGraph[{n + 1, m + 1}, VertexCoordinates -> (Join @@
Array[{sc1, sc2} {#2, #1} &, {m + 1, n + 1}, 0]), opts];
(* ignore the red syntax highligting *)

options = {VertexLabels -> "Name", VertexStyle -> Red,
VertexSize -> Small, Axes -> True, ImagePadding -> 20, AxesOrigin -> {0, 0}};

ggF[5, 5, 16 2.54/5, 16 2.54/5, ImageSize -> 400, options]


ggF[5, 3, 16 2.54/5, 16 2.54/3, ImageSize -> 400, options]


ggF[5, 3, 16 2.54/5, 16 2.54/5, ImageSize -> 400, options]


-

In case you want to preserve the Graphics form, note that the Table used to generate nodes returns the vertical lines of those you seek. Transpose to get the horizontal lines.

For example:

vertlines  = Table[{i (16 2.54)/n, j (16 2.54)/n}, {i, 0, n}, {j, 0, n}];
lines      = Flatten[{vertlines, Transpose@vertlines}, 1];
nodes      = Flatten[vertlines, 1];
(* other defs. as is *)

Show[Graphics[{Line[lines], {Red, nodelabels}, {elementlabels}}], dataPlot,
AspectRatio -> 1, Axes -> True]


Or if you're concerned about efficiency and wish to omit the interior points of the lines, then use

lines = Flatten[{vertlines, Transpose@vertlines}[[All, All, {1, -1}]], 1];

-