Shortest Code to randomly place points and connect them
Java : 318 282 265
Because, ya know, Java:
class M{public static void main(String[]a){new Frame(){public void paint(Graphics g){int i=0,j,d=640,n=25,x[]=new int[n],y[]=x.clone();for(setSize(d,d);i<n;i++)for(j=0,x[i]=(int)(random()*d),y[i]=(int)(random()*d);j<i;g.drawLine(x[i],y[i],x[j],y[j++]));}}.show();}}
Its just a simple loop that makes random dots and draws lines between the current dot and all previous ones.
Example with 25 dots:
With line breaks and imports:
import java.awt.*;
import static java.lang.Math.*;
class M{
public static void main(String[]a){
new Frame(){
public void paint(Graphics g){
int i=0,j,d=640,n=25,x[]=new int[n],y[]=x.clone();
for(setSize(d,d);i<n;i++)
for(j=0,x[i]=(int)(random()*d),y[i]=(int)(random()*d);
j<i;
g.drawLine(x[i],y[i],x[j],y[j++]));
}
}.show();
}
}
Edit: Since we're not counting imports, I imported a couple more things to save some characters later.
Edit 2: OP added an allowance for hardcoding number of dots. -17 chars :)
Matlab (22)
gplot(ones(n),rand(n))
It is assumend that n is the number of points, and it looks like this for n=10:
n=6
:
Explaination
gplot
is a command for plotting graphs. The first argument is a n x n
incidence matrix (full of ones, obviously). The second argument should be a n x 2
matrix with the coordinates of the points, but it does not matter if the second dimension is bigger that 2, so I just generate an n x n
matrix of random values (which is 2 characters shorter than generating an n x 2
matrix).
Links to documentation
- gplot
- ones
- rand
Python 2 - 41 35
After importing some libraries as allowed for this challenge
from pylab import rand as r
from pylab import plot as p
from itertools import product as x
from itertools import chain as c
we can plot some number of connected points with just one line of code:
X=r(5,2);p(*zip(*c(*list(x(X,X)))))
(The screenshot was generated with 10 points.)