@@ -0,0 +1,46 @@ | |||
//fixed 14.01.2017,20.04.2017 am | |||
class IntList{ | |||
int [] data; | |||
int len =0; | |||
IntList(int len) { | |||
data = new int[Math.max(2,len)]; | |||
} | |||
IntList() { | |||
data = new int[16]; // some default size | |||
} | |||
void add(int elem) { | |||
if (len == data.length) { | |||
int [] b = new int [data.length*2]; | |||
System.arraycopy(data,0, b,0,len); | |||
//for (int i = 0; i < len; i++) b[i] = data[i]; | |||
data = b; | |||
} | |||
data[len++] = elem; | |||
}// end add | |||
// adds IntList other to this IntList as last part | |||
void append (IntList other) { | |||
if ( len + other.len > data.length) { | |||
int newLen = Math.max(2*len,len + 2*other.len); | |||
int [] b = new int [newLen]; | |||
System.arraycopy(data,0,b,0,len); | |||
data = b; | |||
} | |||
System.arraycopy(other.data,0, data, len, other.len); | |||
len += other.len; | |||
} // end join other Intlist to this IntList | |||
void clear(){ | |||
len =0; | |||
} // end clear; | |||
int get (int pos){ | |||
if (pos > len-1 ) return -1; else return data [pos]; | |||
}//end get | |||
int size() { | |||
return len; | |||
}//end size | |||
} // end class IntList |
@@ -0,0 +1,8 @@ | |||
class Main { | |||
public static void main(String[] args) { | |||
if (args.length != 1) { | |||
System.out.println("Usage: java Main <n>"); | |||
System.exit(1); | |||
} | |||
} | |||
} |
@@ -0,0 +1,63 @@ | |||
import java.util.Random; | |||
/** | |||
* Class NPunkter17 for aa finne n tilfeldige, ulike punkter i x,y-planet | |||
* ver 7.mai 2015. Reworked to get not cohyll be a square for lage n | |||
************************************************************************/ | |||
public class NPunkter17 { | |||
Random r; | |||
int n; | |||
byte [] bitArr; | |||
static int maxXY, xShift =3; | |||
int scaleFactor = 3; // scaleFactor * scaleFactor * n= antall mulige punkter i planet (her: 3*n) | |||
final int [] bitMask ={1,2,4,8,16,32,64,128}; | |||
int xCentre =0,yCentre=0, maxVal; | |||
NPunkter17(int n) { | |||
this.n =n; | |||
maxXY = Math.max(10,(int) Math.sqrt(n) * scaleFactor); // største X og Y verdi | |||
while ((1<<xShift) < maxXY) xShift++; | |||
xShift = xShift - 3; // 8 bits per byte | |||
bitArr = new byte[(maxXY<<xShift |(maxXY>>3)) + 1]; | |||
r = new Random(123); | |||
// added to get more 'roundish' set of points | |||
for (int i = 1 ; i <=20 ; i++){ | |||
xCentre += r.nextInt(maxXY); | |||
yCentre += r.nextInt(maxXY); | |||
} | |||
xCentre /= 20; | |||
yCentre /= 20; | |||
maxVal = (int)(xCentre*1.33); | |||
} | |||
private void setUsed(int x, int y) { | |||
bitArr[(x<<xShift) | (y >>3)] |= bitMask[(y&7)]; | |||
} | |||
private boolean used (int x, int y) { | |||
return (bitArr[(x<<xShift) | (y >>3)] & bitMask[y&7]) != 0; | |||
} | |||
public void fyllArrayer(int [] x, int[] y) { | |||
int next =0; | |||
int xval, yval; | |||
// new 2017 | |||
while (next < n) { | |||
do{ | |||
xval = r.nextInt(maxXY)+1; | |||
yval = r.nextInt(maxXY)+1; | |||
} while (used (xval, yval) || | |||
Math.abs(xval-xCentre) +Math.abs(yval-yCentre) > maxVal); | |||
x[next] = xval; | |||
y[next] = yval; | |||
setUsed (xval,yval); | |||
next++; | |||
} // next point | |||
}// end fyllArrayer | |||
public IntList lagIntList() { | |||
IntList res = new IntList(n); | |||
for (int i= 0; i <n; i++) res.add(i); | |||
return res; | |||
}// end fyllArrayer | |||
} // end class NPunkter |
@@ -0,0 +1,16 @@ | |||
abstract class Oblig4 { | |||
// Required for TegnUt | |||
public int x[]; | |||
public int y[]; | |||
public int n; | |||
public int MAX_Y; | |||
public int MAX_X; | |||
NPunkter17 points; | |||
Oblig4(NPunkter17 points) { | |||
this.points = points; | |||
} | |||
abstract void solve(); | |||
} |
@@ -0,0 +1,5 @@ | |||
class Parallel extends Oblig4 { | |||
void solve() { | |||
System.out.println("Solving in parallel"); | |||
} | |||
} |
@@ -0,0 +1,5 @@ | |||
class Sequential extends Oblig4 { | |||
void solve() { | |||
System.out.println("Solving sequentially"); | |||
} | |||
} |
@@ -0,0 +1,78 @@ | |||
import java.util.*; | |||
import javax.swing.*; | |||
import java.awt.*; | |||
/** | |||
* Klasse for aa tegne et punktsett med n punkter (Helst n < 200) og | |||
* den konvekse innhyllinga i IntList CoHull, koordinatene i d.x[] og d.y[] | |||
* ver 7.mai 2015, 2016,2017 | |||
******************************************************************************/ | |||
class TegnUt extends JFrame{ | |||
Oblig4 d; | |||
IntList theCoHull; | |||
int n, MAX_Y; | |||
int [] x,y; | |||
TegnUt(Oblig4 d, IntList CoHull, String s ){ | |||
theCoHull = CoHull; | |||
this.d =d; | |||
x = d.x; | |||
y = d.y; | |||
n = d.n; | |||
MAX_Y = d.MAX_Y; | |||
size = 500; | |||
margin = 50; | |||
scale =size/d.MAX_X +0.8; | |||
setTitle("Oblig4, num points:"+n+" "+s); | |||
grafen = new Graph(); | |||
getContentPane().add(grafen, BorderLayout.CENTER); | |||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |||
pack(); | |||
setVisible(true); | |||
// angir foretrukket storrelse paa dette lerretet. | |||
setPreferredSize(new Dimension(d.MAX_X+2*margin,d.MAX_Y+2*margin)); | |||
} | |||
Graph grafen; | |||
int size , margin; | |||
double scale ; | |||
class Graph extends JPanel { | |||
void drawPoint(int p, Graphics g) { | |||
int SIZE =7; | |||
if (n <= 50) g.drawString(p+"("+x[p]+","+y[p]+")",xDraw(x[p])-SIZE/2,yDraw(y[p])-SIZE/2); | |||
else if (n <= 200)g.drawString(p+"",xDraw(x[p])-SIZE/2,yDraw(y[p])-SIZE/2); | |||
g.drawOval (xDraw(x[p])-SIZE/2,yDraw(y[p])-SIZE/2,SIZE,SIZE); | |||
g.fillOval (xDraw(x[p])-SIZE/2,yDraw(y[p])-SIZE/2,SIZE,SIZE); | |||
} | |||
Graph() { | |||
setPreferredSize(new Dimension(size+2*margin+10,size+2*margin+10)); | |||
} | |||
int xDraw(int x){return (int)(x*scale)+ margin ;} | |||
int yDraw(int y){return (int)((MAX_Y-y)*scale+margin);} | |||
public void paintComponent(Graphics g) { | |||
super.paintComponent(g); | |||
g.setColor(Color.black); | |||
for (int i = 0; i < n; i++){ | |||
drawPoint(i,g); | |||
} | |||
g.setColor(Color.red); | |||
// draw cohull | |||
int x2 = x[theCoHull.get(0)], y2 = y[theCoHull.get(0)],x1,y1; | |||
for (int i = 1; i < theCoHull.size(); i++){ | |||
y1 = y2; x1=x2; | |||
x2 = x[theCoHull.get(i)]; | |||
y2 = y[theCoHull.get(i)]; | |||
g.drawLine (xDraw(x1),yDraw(y1), xDraw(x2),yDraw(y2)); | |||
} | |||
g.drawLine (xDraw(x[theCoHull.get(theCoHull.size()-1)]), | |||
yDraw(y[theCoHull.get(theCoHull.size()-1)]), | |||
xDraw(x[theCoHull.get(0)]),yDraw(y[theCoHull.get(0)])); | |||
} // end paintComponent | |||
} // end class Graph | |||
}// end class DT |