postscript programming regular polygon with diagonals - polygon

First of all, I would like to tell You, I have really limited time.
So i would appricate every help with that task.
I have to draw a regular polygon with every diagonal.
( attached file: http://s30.postimg.org/5m6cvd7u9/polygon_with_all_diagonal.png )
Could someone help me with this?
Edit:
Please check scource code:
300 200 translate
/S 28 def
S S scale
4 S div setlinewidth
1 setlinecap
1 setlinejoin
/n 5 def
/r 5 def
newpath
r 0 moveto
1 1 n 1 sub
{
/i exch def
360 n div i mul cos r mul
360 n div i mul sin r mul lineto
} for
closepath
stroke
Thats all what i could do, i have no more idea, how to draw the diagonals.

Cutting and pasting from my other graph drawing routines I get something like this (there is a bit of redundant code but that's because one may want to make the adjacency list on your own):
% routines
/ngon{ [ exch 360 exch div
0 exch 359.9 {
[ exch dup cos 40 mul exch sin 40 mul ]
} for
]
} def
/fmtrx {[ 0 1 4 3 roll 1 sub { [ 1 1 4 3 roll {pop 1} for ] } for ]} def
/drawnodelist { % nodes drawnodelist
{
newpath aload pop 2.5 0 360 arc closepath fill
} forall
} def
/drawedges { % adjacency_matrix nodes drawedges
/drawnodes exch def
dup
/drawlist exch def
/row 0 def
{
/col 0 def
{
pop
drawlist row get col get 1 eq
{
newpath
drawnodes row get aload pop moveto
drawnodes col get aload pop lineto stroke
} if
/col col 1 add def
} forall
/row row 1 add def
} forall
} def
/drawngon {
dup /polygon exch ngon def
polygon drawnodelist % remove this line if you do not want dots
fmtrx polygon drawedges
} def
% call routines
90 rotate
6 drawngon
Here is a sample of different connected polygons produced:

Here is my take:
/n 7 def % number of sides
/R 100 def % radius of circumscribed circle
R 2 mul dup translate
/A 360 n div def
% point given index
/P {
A mul dup
cos R mul
exch
sin R mul
} bind def
0 1 n 1 sub {
dup P 3 2 roll
1 add 1 n 1 sub {
P moveto 2 copy lineto stroke
} for
pop pop
} for
showpage

Arguably a more fitting description of the problem is that you are trying to draw a fully connected graph.
The [Wikipedia-entry on PostScript][http://en.wikipedia.org/wiki/PostScript#Units_of_length] reveals a simple procedure that can be used to solve your problem:
For example, in order to draw a vertical line of 4 cm length, it is
sufficient to type:
0 0 moveto
0 113.385827 lineto stroke
Observe that the above sample can be easily adapted to draw any line; that is, it does not necessarily have to be a vertical line. Now, for each vertex of your polygon draw a line to every other vertex.
Knowing that the polar coordinates of the vertices of a n-polygon will be (rad,i(2π/n)) for i=0..n-1, you can generate the postscript code to describe a polygon path from another language, like C.
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void emitpoly (double x, double y, int n, double r, int up) {
int i;
double a;
printf("%f %f moveto\n", x+r*cos(up?M_PI/2.0:0.0), y+r*sin(up?M_PI/2.0:0.0));
for(i=1; i<n; i++){
a=(double)i*(2*M_PI)/n;
if (up) a += M_PI/2.0;
printf("%f %f lineto\n", x+r*cos(a), y+r*sin(a));
}
printf("stroke\n");
}
void emitpolyweb (double x, double y, int n, double r, int up) {
int i,j;
double a,b;
for(i=0; i<n; i++){
a=(double)i*(2*M_PI)/n;
if (up) a += M_PI/2.0;
printf("%f %f moveto\n", x+r*cos(a), y+r*sin(a));
for(j=0; j<n; j++){
b=(double)j*(2*M_PI)/n;
if (up) b += M_PI/2.0;
printf("%f %f lineto\n", x+r*cos(b), y+r*sin(b));
printf("closepath\n");
}
}
printf("stroke\n");
}
int main(int argc, char**argv) {
int up = 0, n = 3;
double x = 0.0, y = 0.0, r = 1.0;
if (argc>1) x = strtod(argv[1],NULL); // set x from 1st arg, if present
if (argc>2) y = strtod(argv[2],NULL); // set y from 2nd arg, if present
if (argc>3) n = strtol(argv[3],NULL,10); // set n from 3rd arg, if present
if (argc>4) r = strtod(argv[4],NULL); // set r from 4th arg, if present
if (argc>5) up = strcmp(argv[5],"up") == 0; // rotate 90-degrees if 5th arg is "up"
//emitpoly(x,y,n,r,up);
emitpolyweb(x,y,n,r,up);
printf("showpage\n");
return 0;
}

I came up with some results in this area while playing with venn diagrams, and further in geodesic flowers.
This code implements the polygon as a control structure which takes a radius and a repeat count (number of polygon points) and calls the user-defined procedure at each point (by scaling and rotating the CTM). So it's a loop, that makes the user point (1,0) map to successive points of the polygon for each iteration. Using this function, the figure itself is fairly simple. Being a control structure, it is not limited to drawing polygons but can also be used to position smaller drawings at the vertices of a polygon, or even to generate an array of vertex points.
Note also, that this code doesn't have to call sin or cos, because that is handled by rotating the coordinate system. The big trick here is using the scanner to produce an optimized loop body (procedure array).
After placing the init incr max control values on the stack for the for loop, it performs ({...}) token pop exch pop bind end exec which dynamically creates a procedure and executes the call to for. token called (successfully) on a string will yield several values on the stack: an empty string, the scanned-token itself (a ps object of the appropriate type), and the Boolean true. So the pop exch pop just trims the Boolean and the empty string. bind then replaces operator names with the operators themselves, so we perform fewer hash-lookups during the loop. Then end removes our temporary dictionary, before execing the procedure.
This works because all of the variables have been directly substituted by the token operator since they are written as //immediate-names and the temporary dictionary is available on the dict stack when token does this. Removing the dict makes this a true control structure that will not interfere with the meaning of any names used by the user-proc, even if it uses names like s, p, and m.
The generated procedure body {{//s setmatrix rotate //p exec} for //m setmatrix} has the user proc embedded //p and two matrices, //s a matrix pre-scaled by the rad argument and //m a matrix to restore at the end. The for loop body {//s setmatrix rotate //p exec} receives an angle argument as part of the loop control. It resets the CTM to our scaled, oriented matrix //s setmatrix, then rotates by the argument angle rotate, then executes the user-proc //p exec.
%rad n {proc} atpoly -
%call proc having rotated+scaled
%so (1,0) is each vertex of rad-radius n-polygon
/atpoly {
4 dict begin /p exch def /n exch def % rad
/m matrix currentmatrix def
dup scale
/s matrix currentmatrix def
0 360 n div 359.9 %0 dAng maxAng %{}for
({
{
//s setmatrix
rotate
//p exec
} for
//m setmatrix
}) token pop exch pop %instantiate code template
bind
end exec % run loop without dictionary
} bind def
300 400 translate
/rad 100 def
/n 9 def
rad n {
1 0 moveto
1 n {
1 0 lineto
closepath
} atpoly
} atpoly
stroke
To use atpoly, we have to translate to the desired center-point. Then the pseudo-code is:
for each point in poly
moveto point
for each point in poly
lineto point
line back to last moveto point (closepath)
stroke
The are at least two ways to draw just the polygon with atpoly. You can add a redundant moveto at the beginning and use lineto in the proc, or you can use a procedure which does moveto the first time and redefines itself to do lineto thereafter. Here's a variant of the script part that shows both ways, and the interconnected poly.
/rad 100 def
/n 9 def
gsave
400 200 translate
rad n {
1 0 moveto
1 n {
1 0 lineto
closepath
} atpoly
} atpoly
stroke
grestore
gsave
200 200 translate
rad 0 moveto
rad n {
1 0 lineto
} atpoly
closepath stroke
grestore
gsave
200 400 translate
/action { moveto /action { lineto } def } def
rad n {
1 0 action
} atpoly
closepath stroke
grestore
output:
I suppose these blocks could be wrapped up as procedures. These still require the atpoly function from above.
/drawpoly { % rad n x y
gsave
translate
1 index 0 moveto
{
1 0 lineto
} atpoly
closepath stroke
grestore
} def
% rad and n are needed in the outer call to atpoly,
% then n is needed for each iteration.
% To avoid adding a dictionary, duplicate n n-times.
% Once for each iteration.
/drawpolyweb { % rad n x y
gsave
translate % rad n
dup % rad n n
{ dup } repeat % rad n^(n+1)
1 add % rad n^n n+1
-1 roll % n^n rad
%pstack() =
1 index % n^n rad n
{
1 0 moveto % n^(n-i)
1 exch % n^(n-i-1) 1 n
{
1 0 lineto
closepath
} atpoly
} atpoly
stroke
grestore
} def
Then the usage becomes simpler:
%rad n x y
100 9 300 200 drawpoly
100 9 300 500 drawpolyweb

Related

What is the inefficiency in this cairo code using alloc_locals

The following code:
func pow4(n) -> (m : felt):
alloc_locals
local x
jmp body if n != 0
[ap] = 0; ap++
ret
body:
x = n * n
[ap] = x * x; ap++
ret
end
func main():
pow4(n=5)
ret
end
is declared inefficient in the doc because of non-continuous memory.
I ran it and could not see any hole in the memory table:
Addr Value
-----------
⋮
###
⋮
1:0 2:0
1:1 3:0
1:2 5
1:3 1:2
1:4 0:21
1:5 25
1:6 625
so I don't understand where the problem is. I do see a hole with n=0 though:
Addr Value
-----------
⋮
###
⋮
1:0 2:0
1:1 3:0
1:2 0
1:3 1:2
1:4 0:21
⋮
1:6 0
that I can fix using:
jmp body if n != 0
x = 0
ret
but it's not what's suggested:
Move the instruction alloc_locals.
or Use tempvar instead of local.
You are correct that the inefficiency is due to the memory hole, and correct that it appears only for n=0. Holes do not cause an inefficiency just by existing, but rather, their existence usually means that an equivalent code could have executed using fewer memory cells in total (in this case, 6 instead of 7, in the memory section "1:"). To make it more efficient, one should aim to remove the hole (so that the parts before and after it become continuous), whereas your suggested solution just fills it (which the prover does anyway). So your solution would still use 7 memory cells in the memory segment; and will in fact also use 2 additional cells in the program section (for the x=0 command), so it is actually less efficient than leaving the hole empty: compile and check!
The inefficiency in the original code arises from the local variable x being assigned a memory cell even in the case n=0, despite not being used. To make it more efficient we would simply not want to declare and allocate x in this case.
This can be done by moving the alloc_locals command inside "body", so that it isn't executed (and the locals aren't allocated) in the n=0 case, as in the first suggestion: this saves one memory cell in the n=0 case and doesn't affect the n!=0 case. Note that alloc_locals does not have to appear right at the beginning of the code.
The second suggestion is to make x a tempvar instead of a local (and remove alloc_locals entirely). This again means no local variable is allocated in the n=0 case, thus again only 6 memory cells will be used instead of 7. And in fact, because the code is so simple, using the tempvar is also more efficient that declaring it as a local, at least when used as tempvar x = n * n, as it skips merges the ap += 1 command (which is what alloc_locals does in this case) with the x = n * n command, rather than run them separately.
Beyond discussing the theory, you should compile each of these options and compare the lengths of the code and memory segments obtained, and see what is really the most efficient and by how much: this is always true when optimizing, you should always check the actual performance.
So following #dan-carmon answer and for the sake of completeness, here is a summary of the possible implementations and their memory table "1" when n = 0 & n > 0
# n = 0 n = 5
1:0 2:0 1:0 2:0
1:1 3:0 1:1 3:0
1:2 0 1:2 5
1:3 1:2 1:3 1:2
1:4 0:14 1:4 0:14
1:5 0 1:5 25
1:6 625
Note however that the implementation using tempvar uses 2 slots less than the others in the program table "0".
Implementations tested
func pow4_reuse_slot(n) -> (m : felt):
alloc_locals
local x
jmp body if n != 0
x = 0
ret
body:
x = n * n
[ap] = x * x; ap++
ret
end
func pow4_alloc_in_body(n) -> (m : felt):
jmp body if n != 0
[ap] = 0; ap++
ret
body:
alloc_locals
local x
x = n * n
[ap] = x * x; ap++
ret
end
func pow4_use_tempvar(n) -> (m : felt):
jmp body if n != 0
[ap] = 0; ap++
ret
body:
tempvar x = n * n
[ap] = x * x
ret
end

N points are given in Euclidean plane. For each point, find the smallest distance between itself and the other points

The problem is simple: n points are given in Euclidean plane by their coordinates. For each point, you should find the smallest distance between itself and any of the other points, using Euclidean distance. This minimum distance is called the radius for that point. For each point we should return two things:
The radius(r).
The number of points (excluding itself) which have the Euclidean distance less that or equal to 2*r.
Restrictions on the input:
1 <= number of coordinates <= 30000
0 <= x,y <= 10000
Well, I have done this in o(n^2). Does anyone have a better solution ??
Examples:
1. n=3
(0,0)
(0,0)
(3,4)
output-
(0.00 1)
(0.00 1)
(5.00 2)
2. n=5
(5,3)
(7,8)
(0,9)
(3,1)
(4,4)
output-
(1.41 2)
(5.00 4)
(6.40 4)
(2.83 2)
(1.41 1)
First, recognize that distances are symmetric. The distance of i to j is the same as the distance of j to i. In total there are n*(n-1)/2 distances to calculate.
If you pre-compute them with the following loop, then any further processing, like scanning for the closest, is going to be super fast.
// n = number of points
distances=new float[n*(n-1)/2];
int index = 0;
for (int i = 0; i<n-1; i++)
{
for (int j = i+1; j<n; j++)
{
// This is by far the most expensive operation to make:
distances[index++]=Vector2.Distance(Points[i], Points[j]);
}
}
and not to get the distance between two points i and j is fast.
class PointCloud
{
int n;
float[] distances;
float GetDistance(int i, int j)
{
if(i==j) return 0;
if(i>j) return GetDistance(j ,i);
int index = -i*(i-2*n+1)/2+(j-i)-1;
return distances[index];
}
}
The calculation index = -i*(i-2*n+1)/2+(j-i)-1 is explained as follows. We are only storing the upper triangular values of the matrix with the distances. Each row i has row=n-i-1 values, and the index of the first value (closest to the diagonal) is start=sum(n-t-1,t=0,i-1) = -i*(i-2*n+1)/2. The index of the j column is index = start + j- i -1 as the difference in column position between j and the one next to the diagonal.
For example with n=4 there are only 6 values to store. The index for the distance array for each pair calculates as follows:
j=0 j=3
| * 0 1 2 | i=0
| 0 * 3 4 |
| 1 3 * 5 |
| 2 4 5 * | i=3
So to get the distance between i=1 and j=3, the above has index=4 and distances[index] is the needed value. Note that -i*(i-2*n+1)/2+(j-i)-1 = -1*(1-2*4+1)/2+(3-1)-1 = 4
- Reference post: https://stackoverflow.com/a/9040526/380384 for symmetric matrices in packed form, but ones that store the upper triangular values and the diagonals. So, similar but not exact.
Essentially the above cuts the processing by half compared to the naive approach of a double nested loop over all the pairs.

d3js Cluster Force Layout IV block by Mike

I am new to d3js and I'm just starting out.
I am trying the cluster layout example written by Mike in one of his blocks.
https://bl.ocks.org/mbostock/7882658
I got it to work on my machine with my code but I really don't like just blindly copying code without understanding it.
However I am having a tough time understanding the math behind the 'cluster()' and 'collide()' functions and as to how they function.
Could anyone please explain it? Thanks for your help !!
Let's look at each method and I'll comment it as best I can.
Cluster
First the caller:
function tick(e) {
node
.each(cluster(10 * e.alpha * e.alpha)) //for each node on each tick call function returned by cluster function
//pass in alpha cooling parameter to collide
...
I won't rehash an explanation here about how the tick event works. The documentation is clear.
The function:
// returns a closure wrapping the cooling
// alpha (so it can be used for every node on the tick)
function cluster(alpha) {
return function(d) { // d here is the datum on the node
var cluster = clusters[d.cluster]; // clusters is a hash-map, the key is an index of the 10 clusters, the value is an object where d.cluster is the center node in that cluster
if (cluster === d) return; // if we are on the center node, do nothing
var x = d.x - cluster.x, // distance on x of node to center node
y = d.y - cluster.y, // distance on y of node to center node
l = Math.sqrt(x * x + y * y), // distance of node to center node (Pythagorean theorem)
r = d.radius + cluster.radius; // radius of node, plus radius of center node (the center node is always the largest one in the cluster)
if (l != r) { // if the node is not adjacent to the center node
l = (l - r) / l * alpha; //find a length that is slightly closer, this provides the illusion of it moving towards the center on each tick
d.x -= x *= l; // move node closer to center node
d.y -= y *= l;
cluster.x += x; // move center node closer to node
cluster.y += y;
}
};
}
Collide
The collide function is a bit more complicated. Before we dive into it, you need to understand what a QuadTree is and why Bostock is using it. If you want to determine if two elements are colliding the naive algorithm would be to loop the elements both outer and inner to compare each one against every other one. This is, of course, computationally expensive especially on every tick. This is the problem QuadTrees are trying to solve:
A quadtree recursively partitions two-dimensional space into squares, dividing each square into four equally-sized squares. Each distinct point exists in a unique leaf node; coincident points are represented by a linked list. Quadtrees can accelerate various spatial operations, such as the Barnes–Hut approximation for computing many-body forces, collision detection, and searching for nearby points.
What does that mean? First, take a look at this excellent explanation. In my own simplified words it means this: take a 2-d space and divide it into four quadrants. If any quadrant contains 4 or less nodes stop. If the quadrant contains more than four nodes, divide it again into four quadrants. Repeat this until each quadrant/sub-quadrant contains 4 or less nodes. Now when we look for collisions, our inner loop no longer loops nodes, but instead quadrants. If the quadrant doesn't collide then move to the next one. This is a big optimization.
Now onto the code:
// returns a closure wrapping the cooling
// alpha (so it can be used for every node on the tick)
// and the quadtree
function collide(alpha) {
// create quadtree from our nodes
var quadtree = d3.geom.quadtree(nodes);
return function(d) { // d is the datum on the node
var r = d.radius + maxRadius + Math.max(padding, clusterPadding), // r is the radius of the node circle plus padding
nx1 = d.x - r, // nx1, nx2, ny1, ny2 are the bounds of collision detection on the node
nx2 = d.x + r,
ny1 = d.y - r,
ny2 = d.y + r;
quadtree.visit(function(quad, x1, y1, x2, y2) { // visit each quadrant
if (quad.point && (quad.point !== d)) { // if the quadrant is a point (a node and not a sub-quadrant) and that point is not our current node
var x = d.x - quad.point.x, // distance on x of node to quad node
y = d.y - quad.point.y, // distance on y of node to quad node
l = Math.sqrt(x * x + y * y), // distance of node to quad node (Pythagorean theorem)
r = d.radius + quad.point.radius + (d.cluster === quad.point.cluster ? padding : clusterPadding); // radius of node in quadrant
if (l < r) { // if there is a collision
l = (l - r) / l * alpha; // re-position nodes
d.x -= x *= l;
d.y -= y *= l;
quad.point.x += x;
quad.point.y += y;
}
}
// This is important, it determines if the quadrant intersects
// with the node. If it does not, it returns false
// and we no longer visit and sub-quadrants or nodes
// in our quadrant, if true it descends into it
return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
});
};
}

Calculating possible permutations in a grid with the given length?

I have a 4x4 grid full of letters. How can I calculate all possible routes from any point to any point that consist of 2 to 10 points?
All points within a route must be connected to another point within the same route vertically, horizontally or diagonally. For example you can go from A to B, A to E and A to F but not A to C.
Each point can be used only once in a route.
Here's an example of 25 possible permutations:
+---+---+---+---+
| A | B | C | D |
+---+---+---+---+
| E | F | G | H |
+---+---+---+---+
| I | J | K | L |
+---+---+---+---+
| M | N | O | P |
+---+---+---+---+
- AB
- ABC
- ABCD
- ABCDH
- ABCDHG
- ABCDHGF
- ABCDHGFE
- ABCDHGFEI
- ABCDHGFEIJ
- AE
- AEI
- AEIM
- AEIMN
- AEIMNJ
- AEIMNJF
- AIEMNJFB
- AIEMNJFBC
- AIEMNJFBCG
- AFKP
- PONM
- FGKL
- NJFB
- MNJGD
Now I should clear the question. I'm not asking HOW to get all the permutations. I'm asking what is the total amount of the possible permutations (i.e. an integer) and how to calculate it.
As mentioned in the comments the question can be answered with basic DFS in java starting at top left at (0,0)
EDIT: I added if(count(visited)>10) return; for the constraint
static int count=0;
static int count(boolean[][] b){
int r = 0;
for(int i=0;i<b.length;i++){
for(int j=0;j<b[0].length;j++){
if(b[i][j]) r++;
}
}
return r;
}
static boolean[][] copy(boolean[][] arr){
boolean [][] r = new boolean[arr.length][];
for(int i = 0; i < arr.length; i++)
r[i] = arr[i].clone();
return r;
}
static void dfs(int i, int j,boolean[][] visited) {
visited[i][j] = true;
if(count(visited)>10) return;
count++;
for (int k=-1;k<2;k++) {
for (int l=-1;l<2;l++) {
int r = i+k;
int c = j+l;
if (r>-1 && r<visited.length && c>-1 && c<visited.length && !visited[r][c]){
dfs(r,c,copy(visited));
}
}
}
}
public static void main(String args[]) {
boolean[][] visited = {
{false, false, false, false},
{false, false, false, false},
{false, false, false, false},
{false, false, false, false}
};
// dfs(row,column,initialize all to false)
dfs(0,0,visited);
System.out.println(count-1);
}
The above script just goes through each permutation and increments count every time since this includes the starting point (for example (0,0)) i have at the bottom count-1
Output: 105837 (edited from my incorrect original 1012519)
for 2x2 starting at same place i get 15. Which you can see from running
static int count=0;
static int count(boolean[][] b){
int r = 0;
for(int i=0;i<b.length;i++){
for(int j=0;j<b[0].length;j++){
if(b[i][j]) r++;
}
}
return r;
}
static boolean[][] copy(boolean[][] arr){
boolean [][] r = new boolean[arr.length][];
for(int i = 0; i < arr.length; i++)
r[i] = arr[i].clone();
return r;
}
static void dfs(int i, int j,boolean[][] visited,String str) {
visited[i][j] = true;
if (count(visited)>10) return;
count++;
str+="("+i+","+j+")";
System.out.println(str+": "+count);
for (int k=-1;k<2;k++) {
for (int l=-1;l<2;l++) {
int r = i+k;
int c = j+l;
if (r>-1 && r<visited.length && c>-1 && c<visited.length && !visited[r][c]){
dfs(r,c,copy(visited),str);
}
}
}
}
public static void main(String args[]) {
boolean[][] visited = {
{false, false},
{false, false}
};
dfs(0,0,visited,"");
// "count-1" to account for the starting position
System.out.println(count-1);
}
Output:
(0,0): 1
(0,0)(0,1): 2
(0,0)(0,1)(1,0): 3
(0,0)(0,1)(1,0)(1,1): 4
(0,0)(0,1)(1,1): 5
(0,0)(0,1)(1,1)(1,0): 6
(0,0)(1,0): 7
(0,0)(1,0)(0,1): 8
(0,0)(1,0)(0,1)(1,1): 9
(0,0)(1,0)(1,1): 10
(0,0)(1,0)(1,1)(0,1): 11
(0,0)(1,1): 12
(0,0)(1,1)(0,1): 13
(0,0)(1,1)(0,1)(1,0): 14
(0,0)(1,1)(1,0): 15
(0,0)(1,1)(1,0)(0,1): 16
15
the same script with 4x4 instead last 6 lines of output are:
(0,0)(1,1)(2,2)(3,3)(3,2)(3,1)(3,0)(2,1)(1,2)(0,3): 105834
(0,0)(1,1)(2,2)(3,3)(3,2)(3,1)(3,0)(2,1)(1,2)(1,3): 105835
(0,0)(1,1)(2,2)(3,3)(3,2)(3,1)(3,0)(2,1)(1,2)(2,3): 105836
(0,0)(1,1)(2,2)(3,3)(3,2)(3,1)(3,0)(2,1)(2,0): 105837
(0,0)(1,1)(2,2)(3,3)(3,2)(3,1)(3,0)(2,1)(2,0)(1,0): 105838
105837
The requirements for your problem are complex enough that I doubt there is a simple mathematical calculation--at least I cannot think of one. Here is recursive Python code to find your path count.
SIDE = 4 # Length of side of grid
MAXLEN = 10 # Maximum path length allowed
SIDE2 = SIDE + 2
DIRS = ( # offsets for directions
-1 * SIDE2 - 1, # up & left
-1 * SIDE2 + 0, # up
-1 * SIDE2 + 1, # up & right
0 * SIDE2 - 1, # left
0 * SIDE2 + 1, # right
1 * SIDE2 - 1, # down & left
1 * SIDE2 + 0, # down
1 * SIDE2 + 1, # down & right
)
def countpaths(loc, pathlen):
"""Return the number of paths starting at the point indicated by
parameter loc of length at most parameter pathlen, not repeating
points or using points marked False in global variable isfree[]."""
global isfree
pathcnt = 1 # count sub-path of just this one point
if pathlen > 1:
isfree[loc] = False
for dir in DIRS:
if isfree[loc + dir]:
pathcnt += countpaths(loc + dir, pathlen - 1)
isfree[loc] = True
return pathcnt
# Init global boolean array variable to flag which points are still available
isfree = [1 <= r <= SIDE and 1 <= c <= SIDE
for r in range(SIDE2) for c in range(SIDE2)]
# Use the symmetries of the square grid to find count of paths in grid
allpathcnt = 0
for r in range(1, (SIDE + 1) // 2 + 1): # do a triangular slice of the grid
for c in range(1, r + 1):
# Find the number of similar (by symmetry) points in the grid
if 2 * r - 1 == SIDE:
if r == c:
sym = 1 # center of entire grid
else:
sym = 4 # center of column
else:
if r == c:
sym = 4 # diagonal
else:
sym = 8 # other
# Add paths starting at this kind of point removing those of length 1
allpathcnt += sym * (countpaths(r * SIDE2 + c, MAXLEN) - 1)
print('Total path count is ' + str(allpathcnt))
This code takes into account the requirement that paths have lengths between 2 and 10 by limiting the path length to 10 and removing the paths of length 1. The requirement that points are not repeated is fulfilled by using array isfree[] to note which points are still free (True) and which are already used or should not be used (False).
Python is a somewhat slow language, so I increased speed by moving some calculations out of the inner recursions. I used a surrounding border of always-False points around your 4x4 grid, removing the need for explicit bounds checking. I used a one-dimensional list rather than two-dimensional and pre-coded the offsets from each cell to neighboring cells in constant DIRS (for "directions"). I used a final optimization by not using all 16 starting points. There are 4 corner points like A, 8 side points like B, and 4 center points like F, so I just found the numbers of paths from A, B, and F and calculated what the total would be for starting at all points.
This version of my code can handle any size square grid and maximum path length. I checked my code by varying SIDE and MAXLEN separately to 1, 2, and 3, and checking the results for each point by hand.
The final answer I get is
1626144
I was interested to note that the section of code taking the most space is the part that determines the symmetries of a point in the grid. I have found other, more concise ways to do this, but they are all much less readable.

Simple 2D graph drawing - PostScript

Can you tell me an easy way to draw graph(2+x, sin(x), cos(x+3)/3.....) in PS format?
For example, I want to draw f(x) = 2+x, with the following values:
Table of values:
Value of X = -5 | -4 | -3 | -2 | -1 | -0 | 1 .....
Value of Y = -3 | -2 | -1 | 0 | 1 | 2 | 3 .....
How to draw this graph? Draw lineto, draw polygon or use curve command?
What do you think is the best solution?
There are a number of different ways you can do this. If you have a set of coordinates to plot, you can put them in an array, and draw the points while iterating through the array.
/XValues [ -5 -4 -3 -2 -1 0 1 ] def % eg. XValues 0 get ==> -5
/YValues [ -3 -2 -1 0 1 2 3 ] def % YValues 0 get ==> -3
XValues 0 get YValues 0 get % X[0] Y[0]
moveto % move to first point
1 1 XValues length 1 sub { % i push integer i = 1 .. length(XValues)-1 on each iteration
XValues % i XVal push X array
1 index % i XVal i copy i from stack
get % i x get ith X value from array
YValues % i x YVal
2 index % i x YVal i i is 1 position deeper now, so 2 index instead of 1
get % i x y
lineto % i line to next point
pop % discard index variable
} for
Now, of course in Postscript the origin is at the bottom left corner by default, and 72 points make an inch. So these values (-5, -4, -2, etc) are not even going to be visible. So you usually want to start by translating to the center of where you want to draw the graph.
/Center { 300 400 } def % roughly the middle of US letter-size paper
Center translate
Then you want to scale the coordinate system so the graph features are visible. Scalefactor = DesiredSize / ExistingSize.
You could scan the dataset to find the existing size.
/Xmin 1000000 def % really high
/Xmax -1000000 def % really low
XValues { % x forall pushes each X value
dup Xmin lt { % x lower than Xmin?
dup /Xmin exch def % x set Xmin
} if % x
dup Xmax gt { % x higher than Xmax?
/Xmax exch def % set Xmax
}{ % x else (lower than Xmax)
pop % discard X value
} ifelse
} forall
/Datasize Xmax Xmin sub def % X size is (Xmax-Xmin)
6 72 mul DataSize div % scalefactor 6*72/(Xmax-Xmin)
dup % scalefactor scalefactor use same scaling for x and y
scale
But there's a snag when you're doing line-drawing. The width of the lines you draw also depend upon the current coordinate space, so if you scale up the space by a large factor, your lines will become undesirably wide. You can either unscale back to the normal space after you describe the path but before you call stroke. Or, fix the linewidth at the same time you scale.
Since we know how much we've increased the line width (it's the same scaling factor), we can adjust the linewidth graphics parameter in the inverse direction.
1 % push 1 for division
6 72 mul DataSize div % 1 scalefactor 6*72/(Xmax-Xmin)
dup dup % 1 scalefactor scalefactor scalefactor
scale % 1 scalefactor
div % 1/scalefactor
currentlinewidth mul setlinewidth % adjust line width
Now, since this is a graph of a function, we don't actually need the tables of values. We can calculate values on the fly by evaluating the function.
/func { 3 add cos 3 div } def % f(x) = cos(3+x)/3 define function y=f(x)
/Xini -5 def % named loop control parameters
/Xfin 1 def
/Xstep 1 def
Xini dup dup func moveto % moveto first point
Xstep add Xstep Xfin { % x
dup % x x
func % x f(x)
lineto % line to next point
} for
stroke
Finally, if you can take the derivative of the function (create a function which calculates the slope of the original function at each point), then you can use my answer over on TeX.SE to draw the graph with many curve segments instead of lines.

Resources