old projects
This commit is contained in:
BIN
linenSicht/Shape.class
Normal file
BIN
linenSicht/Shape.class
Normal file
Binary file not shown.
34
linenSicht/Shape.java
Normal file
34
linenSicht/Shape.java
Normal file
@@ -0,0 +1,34 @@
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.*;
|
||||
|
||||
public class Shape {
|
||||
|
||||
ArrayList<Integer> x = new ArrayList<Integer>();
|
||||
ArrayList<Integer> y = new ArrayList<Integer>();
|
||||
int len = 0;
|
||||
|
||||
public Shape() {
|
||||
|
||||
}
|
||||
|
||||
public void addPoint(int posX, int posY) {
|
||||
x.add(posX);
|
||||
y.add(posY);
|
||||
len++;
|
||||
|
||||
}
|
||||
|
||||
public int getX(int index) {
|
||||
return x.get(index);
|
||||
}
|
||||
|
||||
public int getY(int index) {
|
||||
return y.get(index);
|
||||
}
|
||||
|
||||
public int getLength() {
|
||||
return len;
|
||||
}
|
||||
}
|
||||
3
linenSicht/file1.txt
Normal file
3
linenSicht/file1.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
1
|
||||
3 535 410 610 70 460 70
|
||||
633 189
|
||||
5
linenSicht/file2.txt
Normal file
5
linenSicht/file2.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
3
|
||||
4 390 260 505 213 551 329 413 444
|
||||
5 410 50 433 50 594 96 525 188 387 165
|
||||
5 170 80 193 80 340 150 331 287 170 402
|
||||
633 189
|
||||
10
linenSicht/file3.txt
Normal file
10
linenSicht/file3.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
8
|
||||
6 539 98 549 98 599 118 569 158 519 198 489 138
|
||||
4 559 178 569 178 609 248 519 238
|
||||
8 389 78 459 68 599 68 479 88 459 178 509 248 599 258 499 298
|
||||
7 320 98 330 98 370 118 360 158 330 198 300 158 280 118
|
||||
7 380 208 390 188 430 208 380 228 390 288 360 248 340 208
|
||||
4 352 287 445 305 386 366 291 296
|
||||
5 319 18 293 53 365 80 238 73 257 15
|
||||
9 637 248 516 330 426 238 462 302 451 350 613 348 761 346 754 231 685 183
|
||||
479 168
|
||||
13
linenSicht/file4.txt
Normal file
13
linenSicht/file4.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
11
|
||||
5 121 39 290 27 284 86 156 110 121 88
|
||||
6 133 206 202 144 254 170 278 224 201 194 156 258
|
||||
6 160 290 247 301 162 398 365 280 276 253 208 233
|
||||
3 170 421 386 298 384 472
|
||||
4 408 297 428 297 565 199 413 475
|
||||
4 300 120 440 160 382 227 320 201
|
||||
3 323 34 440 20 308 85
|
||||
3 500 20 500 140 376 103
|
||||
8 540 20 600 40 600 100 740 100 700 340 660 340 660 140 540 140
|
||||
4 580 240 633 402 896 475 508 466
|
||||
10 780 140 1020 140 1020 480 960 480 960 200 800 200 773 301 900 300 900 340 740 340
|
||||
856 270
|
||||
14
linenSicht/file5.txt
Normal file
14
linenSicht/file5.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
12
|
||||
4 400 185 650 185 650 255 400 255
|
||||
5 497 10 598 19 604 102 549 165 447 141
|
||||
7 320 165 380 165 380 285 510 285 510 305 420 305 320 305
|
||||
4 300 55 360 45 380 135 320 145
|
||||
4 200 35 280 35 280 215 200 215
|
||||
5 170 215 300 265 300 325 180 305 150 245
|
||||
6 90 35 130 95 170 55 180 155 120 195 70 135
|
||||
4 400 125 430 145 410 175 380 155
|
||||
4 90 225 140 225 130 265 90 285
|
||||
5 540 280 541 280 800 280 800 400 540 400
|
||||
10 380 340 381 340 520 340 460 380 540 440 720 460 420 460 140 440 140 300 280 380
|
||||
4 80 300 81 300 140 280 120 360
|
||||
621 162
|
||||
BIN
linenSicht/sichtlinien$1.class
Normal file
BIN
linenSicht/sichtlinien$1.class
Normal file
Binary file not shown.
BIN
linenSicht/sichtlinien$2.class
Normal file
BIN
linenSicht/sichtlinien$2.class
Normal file
Binary file not shown.
BIN
linenSicht/sichtlinien$3.class
Normal file
BIN
linenSicht/sichtlinien$3.class
Normal file
Binary file not shown.
BIN
linenSicht/sichtlinien$Intersect.class
Normal file
BIN
linenSicht/sichtlinien$Intersect.class
Normal file
Binary file not shown.
BIN
linenSicht/sichtlinien$MouseManager.class
Normal file
BIN
linenSicht/sichtlinien$MouseManager.class
Normal file
Binary file not shown.
BIN
linenSicht/sichtlinien$Segment.class
Normal file
BIN
linenSicht/sichtlinien$Segment.class
Normal file
Binary file not shown.
BIN
linenSicht/sichtlinien$Vector.class
Normal file
BIN
linenSicht/sichtlinien$Vector.class
Normal file
Binary file not shown.
BIN
linenSicht/sichtlinien.class
Normal file
BIN
linenSicht/sichtlinien.class
Normal file
Binary file not shown.
469
linenSicht/sichtlinien.java
Normal file
469
linenSicht/sichtlinien.java
Normal file
@@ -0,0 +1,469 @@
|
||||
|
||||
//TODO sterne wegmachen
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.*;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.ArrayList;
|
||||
|
||||
//keys
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
//files
|
||||
import java.io.FileReader;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.BufferedReader;
|
||||
//mouse
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
//refresh
|
||||
import java.awt.event.ActionEvent;
|
||||
import javax.swing.Timer;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class sichtlinien extends JFrame implements KeyListener {
|
||||
|
||||
public static String filename;
|
||||
Scanner scan;
|
||||
Polygon p;
|
||||
Shape[] shapes = makeArr();
|
||||
int amount, startX, startY, HEIGHT = 1100, WIDTH = 1100;
|
||||
|
||||
Timer looper = new Timer(1000 / 60, new ActionListener() {
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
// repaint();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
boolean first = false, showLines = false;
|
||||
|
||||
Segment[] segments = makeSegments();
|
||||
Segment[] rays = createRays();
|
||||
|
||||
public static void main(String[] args) {
|
||||
// DONE for-loop fur mehrere inputs
|
||||
if (args.length != 0) {
|
||||
for (String arg : args) {
|
||||
filename = arg;
|
||||
new sichtlinien(arg);
|
||||
}
|
||||
|
||||
} else {
|
||||
filename = "file1.txt";
|
||||
new sichtlinien("file1.txt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public sichtlinien(String input) {
|
||||
filename = input;
|
||||
System.out.println("Before");
|
||||
setSize(WIDTH, HEIGHT);
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
setBackground(new Color(0, 0, 0));
|
||||
addKeyListener(this);
|
||||
JPanel panel = new JPanel();
|
||||
add(panel);
|
||||
panel.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
System.out.println(e.getX() + "," + e.getY());
|
||||
startX = e.getX();
|
||||
startY = e.getY() - 75;
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
// looper.start();
|
||||
repaint();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
System.out.println("Key: " + e.getKeyChar());
|
||||
if (e.getKeyChar() == 'a') {
|
||||
|
||||
}
|
||||
if (e.getKeyChar() == 'd') {
|
||||
showLines = false;
|
||||
repaint();
|
||||
}
|
||||
if (e.getKeyChar() == 's') {
|
||||
showLines = true;
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
|
||||
}
|
||||
|
||||
public void readFile(String filename) {
|
||||
try {
|
||||
scan = new Scanner(new File(filename));
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public Shape[] makeArr() {
|
||||
readFile(filename);
|
||||
amount = scan.nextInt();
|
||||
System.out.println(amount);
|
||||
Shape shapes[] = new Shape[amount];
|
||||
|
||||
for (int i = 0; i < amount; i++) {
|
||||
shapes[i] = new Shape();
|
||||
System.out.println("added");
|
||||
int aPoints = scan.nextInt();
|
||||
for (int j = 0; j < aPoints; j++) {
|
||||
shapes[i].addPoint(scan.nextInt(), scan.nextInt());
|
||||
System.out.println("added2");
|
||||
System.out.println(shapes[i].getLength());
|
||||
}
|
||||
}
|
||||
startX = scan.nextInt();
|
||||
startY = scan.nextInt();
|
||||
return shapes;
|
||||
}
|
||||
|
||||
public int numberLines() {
|
||||
int out = 0;
|
||||
|
||||
for (Shape shape : shapes) {
|
||||
out = out + shape.getLength();
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
public Segment[] makeSegments() {
|
||||
Segment[] segments = new Segment[numberLines() + 4];
|
||||
int tmpCounter = 0;
|
||||
|
||||
for (Shape shape : shapes) {
|
||||
for (int i = 0; i < shape.getLength(); i++) {
|
||||
if (i < shape.getLength() - 1) {
|
||||
segments[tmpCounter] = new Segment(new Vector(shape.getX(i), shape.getY(i)),
|
||||
new Vector(shape.getX(i + 1), shape.getY(i + 1)));
|
||||
} else {
|
||||
segments[tmpCounter] = new Segment(new Vector(shape.getX(i), shape.getY(i)),
|
||||
new Vector(shape.getX(0), shape.getY(0)));
|
||||
}
|
||||
tmpCounter++;
|
||||
}
|
||||
}
|
||||
|
||||
segments[tmpCounter] = new Segment(new Vector(0, 0), new Vector(WIDTH, 0));
|
||||
tmpCounter++;
|
||||
segments[tmpCounter] = new Segment(new Vector(WIDTH, 0), new Vector(WIDTH, HEIGHT));
|
||||
tmpCounter++;
|
||||
segments[tmpCounter] = new Segment(new Vector(WIDTH, HEIGHT), new Vector(0, HEIGHT));
|
||||
tmpCounter++;
|
||||
segments[tmpCounter] = new Segment(new Vector(0, HEIGHT), new Vector(0, 0));
|
||||
|
||||
return segments;
|
||||
|
||||
}
|
||||
|
||||
Segment[] createRays() {
|
||||
|
||||
rays = new Segment[segments.length * 3];
|
||||
|
||||
int counter = 0;
|
||||
|
||||
for (int s = 0; s < segments.length; s++) {
|
||||
|
||||
Vector a = new Vector(startX, startY);
|
||||
Vector b = segments[s].b;
|
||||
Vector c = b.subtract(a).normalize();
|
||||
|
||||
Segment s1 = new Segment(
|
||||
a,
|
||||
b);
|
||||
Segment s2 = new Segment(
|
||||
a,
|
||||
a.add(c.setDir(c.getAngle() + 0.00001f).normalize().scale(WIDTH)));
|
||||
Segment s3 = new Segment(
|
||||
a,
|
||||
a.add(c.setDir(c.getAngle() - 0.00001f).normalize().scale(WIDTH)));
|
||||
|
||||
rays[counter] = s1;
|
||||
rays[counter + 1] = s2;
|
||||
rays[counter + 2] = s3;
|
||||
|
||||
counter += 3;
|
||||
|
||||
}
|
||||
return (sortRays());
|
||||
}
|
||||
|
||||
Segment[] sortRays() {
|
||||
|
||||
ArrayList<Segment> list = new ArrayList<Segment>();
|
||||
|
||||
for (int r = 0; r < rays.length; r++) {
|
||||
list.add(rays[r]);
|
||||
}
|
||||
|
||||
list.sort(new Comparator<Segment>() {
|
||||
|
||||
@Override
|
||||
public int compare(Segment e1, Segment e2) {
|
||||
|
||||
Vector v1 = e1.b.subtract(e1.a);
|
||||
Vector v2 = e2.b.subtract(e2.a);
|
||||
|
||||
if (v1.getAngle() < v2.getAngle())
|
||||
return -1;
|
||||
if (v1.getAngle() > v2.getAngle())
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
|
||||
rays[i] = list.get(i);
|
||||
|
||||
}
|
||||
return rays;
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
|
||||
createRays();
|
||||
sortRays();
|
||||
|
||||
// TODO farbpalette und farben speichern
|
||||
|
||||
// reset field
|
||||
g.clearRect(0, 0, WIDTH, HEIGHT);
|
||||
|
||||
// shapes
|
||||
g.setColor(new Color(100, 255, 100));
|
||||
drawShapes(g);
|
||||
|
||||
// borders
|
||||
g.setColor(new Color(255, 255, 255));
|
||||
drawBorders(g);
|
||||
|
||||
// TODO fix polygons
|
||||
// make polygons
|
||||
// g.setColor(new Color(100, 100, 200));
|
||||
// drawPolygons(g);
|
||||
|
||||
// make lines
|
||||
g.setColor(new Color(0, 0, 255));
|
||||
drawRays(g);
|
||||
|
||||
// point
|
||||
g.setColor(new Color(255, 100, 100));
|
||||
g.fillOval(startX - 10, startY - 10 + 100, 20, 20);
|
||||
|
||||
}
|
||||
|
||||
void drawBorders(Graphics g) {
|
||||
for (Shape shape : shapes) {
|
||||
p = new Polygon();
|
||||
for (int j = 0; j < shape.getLength(); j++) {
|
||||
p.addPoint(shape.getX(j), shape.getY(j) + 100);
|
||||
}
|
||||
g.drawPolygon(p);
|
||||
}
|
||||
}
|
||||
|
||||
void drawShapes(Graphics g) {
|
||||
for (Shape shape : shapes) {
|
||||
p = new Polygon();
|
||||
for (int j = 0; j < shape.getLength(); j++) {
|
||||
p.addPoint(shape.getX(j), shape.getY(j) + 100);
|
||||
}
|
||||
g.fillPolygon(p);
|
||||
}
|
||||
}
|
||||
|
||||
void drawRays(Graphics g) {
|
||||
for (int r = 0; r < rays.length; r++) {
|
||||
Segment ray = rays[r];
|
||||
|
||||
Vector closestIntersect = ray.b;
|
||||
float distToIntersect = WIDTH;
|
||||
|
||||
for (int i = 0; i < segments.length; i++) {
|
||||
|
||||
Intersect inter = getIntersection(ray, segments[i]);
|
||||
|
||||
if (inter != null) {
|
||||
|
||||
float d = inter.distance;
|
||||
|
||||
if (distToIntersect > d) {
|
||||
distToIntersect = d;
|
||||
closestIntersect = inter.intersect;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ray.b = closestIntersect;
|
||||
}
|
||||
|
||||
for (int r = 0; r < rays.length; r++) {
|
||||
|
||||
Segment ray = rays[r];
|
||||
|
||||
g.drawLine((int) ray.a.x, (int) ray.a.y + 100, (int) ray.b.x, (int) ray.b.y + 100);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void drawPolygons(Graphics g) {
|
||||
|
||||
for (int r = 0; r < rays.length; r++) {
|
||||
|
||||
Segment ray1 = rays[r];
|
||||
Segment ray2;
|
||||
|
||||
if (r == rays.length - 1) {
|
||||
ray2 = rays[0];
|
||||
} else {
|
||||
ray2 = rays[r + 1];
|
||||
}
|
||||
|
||||
Polygon p;
|
||||
int[] x_points = { (int) ray1.a.x, (int) ray1.b.x, (int) ray2.b.x };
|
||||
int[] y_points = { (int) ray1.a.y + 100, (int) ray1.b.y + 100, (int) ray2.b.y + 100 };
|
||||
|
||||
p = new Polygon(x_points, y_points, 3);
|
||||
|
||||
g.fillPolygon(p);
|
||||
|
||||
// g.setClip(p);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void neuzeichnen() {
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void anzeigenTxt() {
|
||||
|
||||
}
|
||||
|
||||
public void wait(int milliseconds) {
|
||||
try {
|
||||
Thread.sleep(milliseconds);
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Intersect getIntersection(Segment s1, Segment s2) {
|
||||
|
||||
Vector p = s1.a;
|
||||
Vector q = s2.a;
|
||||
Vector r = s1.b.subtract(s1.a);
|
||||
Vector s = s2.b.subtract(s2.a);
|
||||
|
||||
float t = (crossProduct(q, s) - crossProduct(p, s)) / crossProduct(r, s);
|
||||
float u = (crossProduct(p, r) - crossProduct(q, r)) / crossProduct(s, r);
|
||||
|
||||
if (t >= 0 && t <= 1 && u >= 0 && u <= 1) {
|
||||
return new Intersect(new Vector(p.x + t * r.x, p.y + t * r.y), t);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
float crossProduct(Vector a, Vector b) {
|
||||
return a.x * b.y - a.y * b.x;
|
||||
}
|
||||
|
||||
class Intersect {
|
||||
Vector intersect;
|
||||
float distance;
|
||||
|
||||
Intersect(Vector i, float d) {
|
||||
this.intersect = i;
|
||||
this.distance = d;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Segment {
|
||||
|
||||
Vector a;
|
||||
Vector b;
|
||||
|
||||
Segment(Vector a, Vector b) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Vector {
|
||||
float x;
|
||||
float y;
|
||||
|
||||
Vector(float x, float y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
Vector add(Vector v) {
|
||||
return new Vector(x + v.x, y + v.y);
|
||||
}
|
||||
|
||||
Vector subtract(Vector v) {
|
||||
return new Vector(x - v.x, y - v.y);
|
||||
}
|
||||
|
||||
float getM() {
|
||||
return (float) (Math.sqrt(x * x + y * y));
|
||||
}
|
||||
|
||||
Vector normalize() {
|
||||
float m = getM();
|
||||
x = x / m;
|
||||
y = y / m;
|
||||
return this;
|
||||
}
|
||||
|
||||
Vector scale(float t) {
|
||||
x *= t;
|
||||
y *= t;
|
||||
return this;
|
||||
}
|
||||
|
||||
Vector setDir(float angle) {
|
||||
|
||||
float m = getM();
|
||||
return new Vector((float) (m * Math.cos(angle)), (float) (m * Math.sin(angle)));
|
||||
|
||||
}
|
||||
|
||||
float getAngle() {
|
||||
return (float) Math.atan2(y, x);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user