# Writing a program that calculates midpoints based on randomly picking vertices

• May 16th, 2013, 01:31 PM
codehelppl0x
Writing a program that calculates midpoints based on randomly picking vertices
What I want to do is start with three points that make an equilateral triangle. My code should pick two of the three points at random, calculate a midpoint (m), and plot it. Then from the midpoint m it generated, the code will pick another one of the three original points at random and compute a new midpoint (m2). The last step should be repeated 10,000 times. I'm just starting out with Java and am really lost and confused at this point. This is the code I have so far (please feel free to point out any mistakes I made in the code I have!):

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Dimension;

public class Game
{
static final int HEIGHT = 500;
static final int WIDTH = 500;

public static void main(String[] args)
{
JFrame frame = new JFrame("The Game");
Board board = new Board(WIDTH, HEIGHT);
Point p1 = new Point(0,0);
Point p2 = new Point(500, 0);
Point p3 = new Point(0, 250);

frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setContentPane(board);

frame.pack();
frame.setVisible(true);

}
}

class Board extends JPanel
{
public Board(int h, int w)
{
setPreferredSize(new Dimension(w, h));
}

{
ArrayList <Point> point = new ArrayList();
}

public void paint(Graphics g)
{
super.paint(g);

int i = 0;

while (i < 10000)
{
i++;
}

}
}

class Point
{
int x;
int y;

public Point(int x, int y)
{
x = this.x;
y = this.y;
}

private static Point midPoint(Point p1, Point p2)
{
return new Point((p1.x + p2.x)/2, (p1.y + p2.y)/2);
}

public double getX()
{
return x;
}

public double getY()
{
return y;
}

}
• May 16th, 2013, 04:31 PM
Parranoia
Re: Writing a program that calculates midpoints based on randomly picking vertices
In the future please wrap your code in the code tags to make it easier to read.
What part are you stuck on exactly? What are you not able to figure out?
• May 18th, 2013, 08:32 PM
theoriginalanomaly
Re: Writing a program that calculates midpoints based on randomly picking vertices
I imagine you are trying to plot a Sierpinski Triangle Fractal

Code java:

```public void paint(Graphics g) { super.paint(g);   int i = 0;   while (i < 10000) { board.add(); i++; }```

Code java:

```public Point(int x, int y) { x = this.x; y = this.y; }   // It should be the other way around   public Point(int x, int y){ this.x = x; this.y = y; }   class Point { int x; int y;   public Point(int x, int y) { this.x = x; this.y = y; }   // Would need to be public, and no need to make it static, also you can drop the second // Point for a argument as you can use your currentPosition point as the this.x this.y .   public Point midPoint(Point p1) { return new Point((p1.x + x) / 2, (p1.y + y) / 2); } // Return ints for the drawRect method public int getX() { return x; }   public int getY() { return y; }   }```
Read this to see why you should use paintComponent
A Closer Look at the Paint Mechanism (The Java™ Tutorials > Creating a GUI With JFC/Swing > Performing Custom Painting)

Code java:

```import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel;   class Board extends JPanel { Point currentPosition, temp; Point topVertex, bottomLeftVertex, bottomRightVertex; int random;   public Board(int h, int w) { // setPreferredSize(new Dimension(w, h)); setSize(w, h);   // Math.random() returns a double between 0 and 1, cast it as an int // Needs to be a pixel location within your JPanel hence * w and * h. Add 1 // It always rounds down when casting to an int, so need to add 1 currentPosition = new Point((int) (Math.random() * w) + 1, (int) (Math.random() * h) + 1);   // set the Vertex Points topVertex = new Point(w / 2, 0); bottomLeftVertex = new Point(0, h); bottomRightVertex = new Point(w, h);     }   public void paintComponent(Graphics g){ super.paintComponents(g);     for (int i = 0; i < 100000; i++){ // For each iteration get a random number 1>= and >=3 random = (int) (Math.random() * 3 + 1);   // Random assignment of Vertex points to the random number generated if (random == 1){ // Cause I can g.setColor(Color.BLACK); // Get the Point to be Plotted, Notice the currentPosition is calling the method temp = currentPosition.midPoint(topVertex); // To act as a point g.fillRect(temp.getX(), temp.getY(), 1, 1); // Make sure to reassign the currentPosition currentPosition = temp; }   if (random == 2){ g.setColor(Color.RED); temp = currentPosition.midPoint(bottomLeftVertex); g.fillRect(temp.getX(), temp.getY(), 1, 1); currentPosition = temp; }   if (random == 3){ g.setColor(Color.GREEN); temp = currentPosition.midPoint(bottomRightVertex); g.fillRect(temp.getX(), temp.getY(), 1, 1); currentPosition = temp; } }   }   /* * public void addPoint(Point p) { ArrayList<Point> point = new ArrayList(); * points.add(p); } * * public void paint(Graphics g) { super.paint(g); * * //int i = 0; * * /* while (i < 10000) { // ?? board.add(); i++; } * * } */ }```
• May 23rd, 2013, 06:17 AM
diyaots
Re: Writing a program that calculates midpoints based on randomly picking vertices
Thanks codehelppl0x,
Very nice explain! :)