improving drawing pythagoras tree - java

I have written program for drawing pythagoras tree fractal. Can anybody see any way of improving it ? Now it is 89 LOC.
import java.awt.*;
import java.util.Scanner;
import javax.swing.*;
public class Main extends JFrame {;
public Main(int n) {
setSize(900, 900);
setTitle("Pythagoras tree");
add(new Draw(n));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Give amount of steps: ");
new Main(sc.nextInt());
}
}
class Draw extends JComponent {
private int height = 800;
private int width = 800;
private int steps;
public Draw(int n) {
steps = n;
Dimension d = new Dimension(width, height);
setMinimumSize(d);
setPreferredSize(d);
setMaximumSize(d);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
g.setColor(Color.black);
int x1, x2, x3, y1, y2, y3;
int base = width/7;
x1 = (width/2)-(base/2);
x2 = (width/2)+(base/2);
x3 = width/2;
y1 = (height-(height/15))-base;
y2 = height-(height/15);
y3 = (height-(height/15))-(base+(base/2));
g.drawPolygon(new int[]{x1, x1, x2, x2, x1}, new int[]{y1, y2, y2, y1, y1}, 5);
int n1 = steps;
if(--n1 > 0){
g.drawPolygon(new int[] {x1, x3, x2}, new int[] {y1, y3, y1}, 3);
paintMore(n1, g, x1, x3, x2, y1, y3, y1);
paintMore(n1, g, x2, x3, x1, y1, y3, y1);
}
}
public void paintMore(int n1, Graphics g, double x1_1, double x2_1, double x3_1, double y1_1, double y2_1, double y3_1){
int x1, x2, x3, y1, y2, y3;
x1 = (int)(x1_1 + (x2_1-x3_1));
x2 = (int)(x2_1 + (x2_1-x3_1));
x3 = (int)(((x2_1 + (x2_1-x3_1)) + ((x2_1-x3_1)/2)) + ((x1_1-x2_1)/2));
y1 = (int)(y1_1 + (y2_1-y3_1));
y2 = (int)(y2_1 + (y2_1-y3_1));
y3 = (int)(((y1_1 + (y2_1-y3_1)) + ((y2_1-y1_1)/2)) + ((y2_1-y3_1)/2));
g.setColor(Color.green);
g.drawPolygon(new int[] {x1, x2, (int)x2_1, x1}, new int[] {y1, y2, (int)y2_1, y1}, 4);
g.drawLine((int)x1, (int)y1, (int)x1_1, (int)y1_1);
g.drawLine((int)x2_1, (int)y2_1, (int)x2, (int)y2);
g.drawLine((int)x1, (int)y1, (int)x2, (int)y2);
if(--n1 > 0){
g.drawLine((int)x1, (int)y1, (int)x3, (int)y3);
g.drawLine((int)x2, (int)y2, (int)x3, (int)y3);
paintMore(n1, g, x1, x3, x2, y1, y3, y2);
paintMore(n1, g, x2, x3, x1, y2, y3, y1);
}
}
}

Use drawPolygon instead of multiply drawLine
Remove the unused method pow,
Clean up your imports
Remove the unnecessary variables h and w
put --n > 0 into a one line
do some reformatting
and there you go, 90 lines (comments still counted):
import java.awt.*;
import java.util.Scanner;
import javax.swing.*;
public class Main extends JFrame {;
public Main(int n) {
setSize(900, 900);
setTitle("Pythagoras tree");
add(new Draw(n));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Give amount of steps: ");
new Main(sc.nextInt());
}
}
class Draw extends JComponent {
private int height = 800;
private int width = 800;
private int steps;
public Draw(int n) {
steps = n;
Dimension d = new Dimension(width, height);
setMinimumSize(d);
setPreferredSize(d);
setMaximumSize(d);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
g.setColor(Color.black);
int x1, x2, x3, y1, y2, y3;
int base = width/7;
x1 = (width/2)-(base/2);
x2 = (width/2)+(base/2);
x3 = width/2;
y1 = (height-(height/15))-base;
y2 = height-(height/15);
y3 = (height-(height/15))-(base+(base/2));
//paint
g.drawPolygon(new int[]{x1, x1, x2, x2, x1}, new int[]{y1, y2, y2, y1, y1}, 5);
int n1 = steps;
if(--n1 > 0){
g.drawPolygon(new int[] {x1, x3, x2}, new int[] {y1, y3, y1}, 3);
paintMore(n1, g, x1, x3, x2, y1, y3, y1);
paintMore(n1, g, x2, x3, x1, y1, y3, y1);
}
}
public void paintMore(int n1, Graphics g, double x1_1, double x2_1, double x3_1, double y1_1, double y2_1, double y3_1){
double x1, x2, x3, y1, y2, y3;
//counting
x1 = x1_1 + (x2_1-x3_1);
x2 = x2_1 + (x2_1-x3_1);
x3 = ((x2_1 + (x2_1-x3_1)) + ((x2_1-x3_1)/2)) + ((x1_1-x2_1)/2);
y1 = y1_1 + (y2_1-y3_1);
y2 = y2_1 + (y2_1-y3_1);
y3 = ((y1_1 + (y2_1-y3_1)) + ((y2_1-y1_1)/2)) + ((y2_1-y3_1)/2);
//paint
g.setColor(Color.green);
g.drawPolygon(new int[] {(int)x1, (int)x2, (int)x2_1, (int)x1},
new int[] {(int)y1, (int)y2, (int)y2_1, (int)y1}, 4);
g.drawLine((int)x1, (int)y1, (int)x1_1, (int)y1_1);
g.drawLine((int)x2_1, (int)y2_1, (int)x2, (int)y2);
g.drawLine((int)x1, (int)y1, (int)x2, (int)y2);
if(--n1 > 0){
g.drawLine((int)x1, (int)y1, (int)x3, (int)y3);
g.drawLine((int)x2, (int)y2, (int)x3, (int)y3);
paintMore(n1, g, x1, x3, x2, y1, y3, y2);
paintMore(n1, g, x2, x3, x1, y2, y3, y1);
}
}
}

Rewrite it in 25 lines of F#? ;-)

replace:
private int pow(int n){
int pow = 2;
for(int i = 1; i < n; i++){
if(n==0){
pow = 1;
}
pow = pow*2;
}
return pow;
}
with
private int pow2(int n) { //Better name, to avoid confusion
return 1 << n;
}
Although I can't see where you use this function.

There are many ways to shorten the code (and the suggestions from Eric and Mihir are a good start), but I would focus on clarity. Variables like x1 and y1 scream for a Point struct to hold these pairs together. You could then pass these points in an array.

Related

Java GUI draw tree using random number

How can I make this tree's angle and depth to be random by using 'random number'?
In the below code JFrame is been used. The intent behind the question is to get the idea of randomizing the angles and the depth, which is passed in the paint method.
public class DrawTreeFrame extends JFrame {
public DrawTreeFrame() {
setSize(800, 700);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private void drawTree(Graphics g, int x1, int y1, double angle, int depth) {
if(depth==0)
return;
int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 10.0);
int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 10.0);
g.drawLine(x1, y1, x2, y2);
drawTree(g, x2, y2, angle-20, depth-1);
drawTree(g, x2, y2, angle+20, depth-1);
}
#Override
public void paint(Graphics g) {
g.setColor(Color.BLACK);
drawTree(g, 400, 600, -90, 10);
}
public static void main(String[] args) {
new DrawTreeFrame();
}
}
You can use Math.random(). The following code method will give you random numbers in a range;
public int getRandomNumber(int min, int max) {
return (int) ((Math.random() * (max - min)) + min);
}
Ultimately your code should look like this:-
class DrawTreeFrame extends JFrame {
public DrawTreeFrame() {
setSize(800, 700);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private void drawTree(Graphics g, int x1, int y1, double angle, int depth) {
if(depth==0)
return;
int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 10.0);
int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 10.0);
g.drawLine(x1, y1, x2, y2);
drawTree(g, x2, y2, angle-20, depth-1);
drawTree(g, x2, y2, angle+20, depth-1);
}
#Override
public void paint(Graphics g) {
g.setColor(Color.BLACK);
int x1 = getRandomNumber(100, 400);
int y1 = getRandomNumber(400, 800);
double angle = getRandomNumber(-10, -100);
int depth = getRandomNumber(5, 20);
drawTree(g, x1, y1, angle, depth);
}
public int getRandomNumber(int min, int max) {
return (int) ((Math.random() * (max - min)) + min);
}
public static void main(String[] args) {
new DrawTreeFrame();
}
}

JavaFX not rendering earlier items in an array, unable to find issue, believe threading might be the issue

I wrote a line drawing algorithm that functions correctly when calculating each point, however when it gets rendered the program skips nearly 20 points, unable to find the issue.
I have tried to mess with the algorithm, specifically the for loop that adds the points to the positions list where the line gets calculated as I have had problems with that in other versions of the code. I just cannot for the life of me find the issue with the code, I am pretty new to Java as you will probably tell from the mess of code below. It says not to post an entire file but I am unable to pinpoint where it is going wrong. I am so incredibly sorry for the mess of code below I just am at that point where I have no clue what is wrong.
package sample;
import com.sun.jdi.ArrayReference;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.util.ArrayList;
class PositionCounter {
public int position;
public void setPosition(int newPos){
position = newPos;
}
}
public class Main extends Application {
// public void drawRectangle(int x1, int y1, int x2, int y2, Stage primStage, GraphicsContext gc){
// drawLine(x1, y2, x2, y2 , primStage, gc);
// drawLine(x1, y1, x2, y2, primStage, gc);
// drawLine(x1, y1, x1 , y2, primStage, gc);
// drawLine(x2, y1, x2 , y2, primStage, gc);
// drawLine(x1, y1, x2, y1 , primStage, gc);
//
// }
//
public void fillPosition(ArrayList<Integer[]> points, GraphicsContext gc, int positionInArray ){
gc.fillRect(points.get(positionInArray)[0], points.get(positionInArray)[1], squareWidth / 2, squareWidth / 2);
//System.out.println("(" + points.get(positionInArray)[0] + ", " + points.get(positionInArray)[1]);
}
public void drawLine(int x1, int y1, int x2, int y2, Stage primStage, GraphicsContext gc){
ArrayList<Integer[]> points = calculateLine(x1, y1, x2, y2, gc);
PositionCounter position = new PositionCounter();
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
Runnable updater = new Runnable() {
#Override
public void run() {
fillPosition(points, gc, position.position);
}
};
int dy = Math.abs(y2 - y1);
int dx = Math.abs(x2 - x1);
int m = (dy >= dx) ? dy : dx;
for (int i = 0; i <= points.size(); i++) {
position.setPosition(i);
try {
Thread.sleep(20);
} catch (InterruptedException ex) {
}
Platform.runLater(updater);
}
}
});
thread.setDaemon(true);
thread.start();
}
#Override
public void start(Stage primaryStage) throws Exception{
//STUFF
Group root = new Group();
Scene scene = new Scene(root, 400, 400);
scene.setFill(Color.GOLD);
Canvas canvas = new Canvas(400, 400);
GraphicsContext gc = canvas.getGraphicsContext2D();
root.getChildren().add(canvas);
//////////
gc.fillRect(0, 200, 400, 1);
gc.fillRect(0, 100, 400, 1);
gc.fillRect(0, 300, 400, 1);
gc.fillRect(200, 0, 1, 400);
gc.fillRect(100, 0, 1, 400);
gc.fillRect(300, 0, 1, 400);
// drawRectangle(50, 50, 350, 250, primaryStage, gc);
//drawLine(50, 250, 350, 250 , primaryStage, gc);
//drawLine(50, 50, 350, 250, primaryStage, gc);
//drawLine(50, 50, 50 , 250, primaryStage, gc);
//drawLine(350, 50, 350 , 250, primaryStage, gc);
drawLine(50, 50, 350, 50 , primaryStage, gc);
//////////
primaryStage.setScene(scene);
primaryStage.show();
/////////
}
int squareWidth = 2;
public ArrayList<Integer[]> calculateLine(int x1, int y1, int x2, int y2, GraphicsContext gc){
ArrayList<Integer[]> points = new ArrayList<Integer[]>();
int dy = Math.abs(y2 - y1);
int dx = Math.abs(x2 - x1);
int sx = (x1 < x2) ? 1 : -1;
int sy = (y1 < y2) ? 1 : -1;
int err = dx-dy;
int x = x1;
int y = y1;
int e2 = 0;
Integer[] pos = new Integer[2];
pos[0] = x;
pos[1] = y;
points.add(pos);
int m = (dy >= dx) ? dy : dx ;
for (int i = 0; i <= m; i++) {
e2 = 2 * err;
if (e2 > -dy) {
err = err - dy;
x = x + sx;
Integer[] position = new Integer[2];
position[0] = x;
position[1] = y;
points.add(position);
}
if (e2 < dx) {
err = err + dx;
y = y + sy;
Integer[] position = new Integer[2];
position[0] = x;
position[1] = y;
points.add(position);
}
}
return points;
}
public static void main(String[] args) {
launch(args);
}
}
The problem is that the calculate line function returns the correct points, it just doesnt render them properly.
I would suggest you use Timeline instead of Thread. It can be done using a Thread, but Timeline is designed for cases like this.
Draw Line Method:
public void drawLine(int x1, int y1, int x2, int y2, Stage primStage, GraphicsContext gc)
{
ArrayList<Integer[]> points = calculateLine(x1, y1, x2, y2, gc);
PositionCounter position = new PositionCounter();
int dy = Math.abs(y2 - y1);
int dx = Math.abs(x2 - x1);
int m = (dy >= dx) ? dy : dx;
AtomicInteger counter = new AtomicInteger();
Timeline animationTimeline = new Timeline(new KeyFrame(Duration.millis(20), (ActionEvent t) -> {
position.setPosition(counter.getAndIncrement());
fillPosition(points, gc, position.position);
}));
animationTimeline.setCycleCount(points.size());
animationTimeline.play();
}
Full Code:
import com.sun.jdi.ArrayReference;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.util.Duration;
class PositionCounter
{
public int position;
public void setPosition(int newPos)
{
position = newPos;
}
}
public class JavaFXTestingGround extends Application
{
// public void drawRectangle(int x1, int y1, int x2, int y2, Stage primStage, GraphicsContext gc){
// drawLine(x1, y2, x2, y2 , primStage, gc);
// drawLine(x1, y1, x2, y2, primStage, gc);
// drawLine(x1, y1, x1 , y2, primStage, gc);
// drawLine(x2, y1, x2 , y2, primStage, gc);
// drawLine(x1, y1, x2, y1 , primStage, gc);
//
// }
//
public void fillPosition(ArrayList<Integer[]> points, GraphicsContext gc, int positionInArray)
{
gc.fillRect(points.get(positionInArray)[0], points.get(positionInArray)[1], squareWidth / 2, squareWidth / 2);
//System.out.println("(" + points.get(positionInArray)[0] + ", " + points.get(positionInArray)[1]);
}
public void drawLine(int x1, int y1, int x2, int y2, Stage primStage, GraphicsContext gc)
{
ArrayList<Integer[]> points = calculateLine(x1, y1, x2, y2, gc);
PositionCounter position = new PositionCounter();
int dy = Math.abs(y2 - y1);
int dx = Math.abs(x2 - x1);
int m = (dy >= dx) ? dy : dx;
AtomicInteger counter = new AtomicInteger();
Timeline animationTimeline = new Timeline(new KeyFrame(Duration.millis(20), (ActionEvent t) -> {
position.setPosition(counter.getAndIncrement());
fillPosition(points, gc, position.position);
}));
animationTimeline.setCycleCount(points.size());
animationTimeline.play();
}
#Override
public void start(Stage primaryStage) throws Exception
{
//STUFF
Group root = new Group();
Scene scene = new Scene(root, 400, 400);
scene.setFill(Color.GOLD);
Canvas canvas = new Canvas(400, 400);
GraphicsContext gc = canvas.getGraphicsContext2D();
root.getChildren().add(canvas);
//////////
gc.fillRect(0, 200, 400, 1);
gc.fillRect(0, 100, 400, 1);
gc.fillRect(0, 300, 400, 1);
gc.fillRect(200, 0, 1, 400);
gc.fillRect(100, 0, 1, 400);
gc.fillRect(300, 0, 1, 400);
// drawRectangle(50, 50, 350, 250, primaryStage, gc);
//drawLine(50, 250, 350, 250 , primaryStage, gc);
//drawLine(50, 50, 350, 250, primaryStage, gc);
//drawLine(50, 50, 50 , 250, primaryStage, gc);
//drawLine(350, 50, 350 , 250, primaryStage, gc);
drawLine(50, 50, 350, 50, primaryStage, gc);
//////////
primaryStage.setScene(scene);
primaryStage.show();
/////////
}
int squareWidth = 2;
public ArrayList<Integer[]> calculateLine(int x1, int y1, int x2, int y2, GraphicsContext gc)
{
ArrayList<Integer[]> points = new ArrayList<Integer[]>();
int dy = Math.abs(y2 - y1);
int dx = Math.abs(x2 - x1);
int sx = (x1 < x2) ? 1 : -1;
int sy = (y1 < y2) ? 1 : -1;
int err = dx - dy;
int x = x1;
int y = y1;
int e2 = 0;
Integer[] pos = new Integer[2];
pos[0] = x;
pos[1] = y;
points.add(pos);
int m = (dy >= dx) ? dy : dx;
for (int i = 0; i <= m; i++) {
e2 = 2 * err;
if (e2 > -dy) {
err = err - dy;
x = x + sx;
Integer[] position = new Integer[2];
position[0] = x;
position[1] = y;
points.add(position);
}
if (e2 < dx) {
err = err + dx;
y = y + sy;
Integer[] position = new Integer[2];
position[0] = x;
position[1] = y;
points.add(position);
}
}
return points;
}
public static void main(String[] args)
{
launch(args);
}
}

How to draw a Polygon (triangle and pentagon) with a mouse?

How can I find xpoints[] and ypoints[], if I want to draw a polygon with the mouse, using getX() and getY() ?
My code at the moment is:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Poligonos extends Figura{
public void Poligonos (int[] xPoints, int[] yPoints, int nPoints){
//private int[] xPoints = {(x1/2), x1, (x1+(x1/2))} // {(getX()/2), getX(), (getX()+(getX()/2))};
//private int[] yPoints = {( y1 + y1 ), y1 ,( y1 + y1 )};
}
#Override
public void desenha(Graphics g) {
g.setColor(cor);
g.drawPolygon( xPoints, yPoints, 3);
}
#Override
public void setCoordenadas(int x1, int y1, int x2, int y2) {
p.x = Math.min(x1, x2);
p.y = Math.min(y1, y2);
int xPoints[] = {(p.x /2), p.x , ( p.x +( p.x /2))}; // {(getX()/2), getX(), (getX()+(getX()/2))};
int yPoints[] = {( p.y + p.y ), p.y ,( p.y + p.y )};
}
}
And getX() and getY() part is:
#Override
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
}
#Override
public void mouseDragged(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
r.setCoordenadas(x1, y1, x2, y2);
pEdicao.repaint();
}
How can I make this work? I just want draw a pentagon and a triangle with mouse.
Thanks for your time.
You could make a setup where you do something like
int numCoordinates = /*(get the number of coordinates by getting number of clicks after a certain action, etc.)*/
int[] xCords = new int[numCoordinates];
int[] yCords = new int[numCoordinates];
xCords[index] = e.getX();
yCords[index] = e.getY();
index++;
if(index > numCoordinates){
index = 0;
pEdicao.repaint();
}

how to convert code into recursion?

import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class DrawIt
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
final int width = 400;
final int height = 400;
frame.setSize(width,height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent component = new JComponent()
{
public void paintComponent(Graphics graph){
draw(graph);
}
};
frame.add(component);
frame.setVisible(true);
}
public static void draw(Graphics g)
{
int x1=100;
int y1 = 100;
int length = 10;
for(int i=0;i<=10;i++)
{
int x2 = x1 + length;
int y2 = y1;
g.drawLine(x1,y1,x2,y2);
x1=x2;
y1=y2;
y2=y1-length;
g.drawLine(x1,y1,x2,y2);
x1=x2;
y1=y2;
length+=10;
x2=x1-length;
g.drawLine(x1, y1, x2, y2);
x1=x2;
y1=y2;
y2=y1+length;
g.drawLine(x1, y1, x2, y2);
x1=x2;
y1=y2;
length+=10;
}
}
}
How do I convert this to recursion? This gives me rectangular spiral but not recursive way. Help. if this way I am using 4 variables, with recursion, how will it be?
public class DrawIt {
public static void main(String[] args) {
JFrame frame = new JFrame();
final int width = 400;
final int height = 400;
frame.setSize(width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent component = new JComponent() {
public void paintComponent(Graphics graph) {
draw(graph);
}
};
frame.add(component);
frame.setVisible(true);
}
public static void drawpuzzle(Graphics g, int x1, int y1, int length, int count) {
if (count> 0) {
int x2 = x1 + length;
int y2 = y1;
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
y2 = y1 - length;
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
length += 10;
x2 = x1 - length;
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
y2 = y1 + length;
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
length += 10;
drawpuzzle(g, x1, y1, length, count - 1);
}
}
public static void draw(Graphics g) {
int x1 = 100;
int y1 = 100;
int length = 10;
int count = 10;
drawpuzzle(g, x1, y1, length, count);
}
}

Cover polygon with rectangles Java

I need to cover some polygon with rectangles here's an example :
The black figure in a black square is the polygon that i need to cover with those green rectangles but i need to do it more efficiently that just make a net like i did. Because as you can see there can be place more green rectangles if i moved them.
Rectangles inside are fixed size(just not as big as polygon it self), one for all like in the picture, they can be places vertically and horizontally, i want to fill the polygon as much as it can fit it inside of it, this polygon is just for example, there can be different polygons with holes in them for example that black small square is a hole.
module = rectangle
private void coverWithModules(Graphics g, int[] xpoints, int[] ypoints) {
Polygon module;
int x1, x2, x3, x4, y1, y2, y3, y4;
int moduleRowNumber = 0;
int totalRows = (getMax(ypoints) / moduleHeight);
while (moduleRowNumber < totalRows) {
// first module
x1 = getMin(xpoints);
y1 = getMin(ypoints) + distance * moduleRowNumber + moduleHeight
* moduleRowNumber;
x2 = x1 + moduleWidth;
y2 = y1;
x3 = x1 + moduleWidth;
y3 = y1 + moduleHeight;
x4 = x1;
y4 = y1 + moduleHeight;
int[] x = { x1, x2, x3, x4 };
int[] y = { y1, y2, y3, y4 };
module = new Polygon();
// check if point are inside the polygon
checkModulePlacement(g, x, y, module);
// placing modules in a row
while (x1 < getMax(xpoints)) {
x1 = x2 + distance;
y1 = getMin(ypoints) + distance * moduleRowNumber
+ moduleHeight * moduleRowNumber;
x2 = x1 + moduleWidth;
y2 = y1;
x3 = x1 + moduleWidth;
y3 = y1 + moduleHeight;
x4 = x1;
y4 = y1 + moduleHeight;
int[] xx = { x1, x2, x3, x4 };
int[] yy = { y1, y2, y3, y4 };
module = new Polygon();
checkModulePlacement(g, xx, yy, module);
}
moduleRowNumber++;
}
}
private void checkModulePlacement(Graphics g, int[] x, int[] y, Polygon module) {
boolean pointInside = true;
boolean pointOnObstraction = true;
for (int i = 0; i < x.length; i++) {
if (pointInside) {
pointInside = roof.contains(x[i], y[i]);
}
module.addPoint(x[i], y[i]);
}
pointOnObstraction = checkForObstractions(module);
g.setColor(Color.GREEN);
if (pointInside == true && pointOnObstraction == false ) {
g.drawPolygon(module);
}
}
I was looking for something and i have found Something like this maybe there is more stuff like this ?
I don't know where to search for such info. What should i look up to get what i need ? Maybe there is some kind of library for this kind of things ?

Categories