You are on page 1of 5

Plot Points randomly

package positioning;
import java.awt.*;
import javax.swing.*;
import java.util.List;
import java.util.*;

public class Positioning extends JPanel


{
static List<Point> list = new ArrayList<Point>();
static List<Points> intersection = new ArrayList<Points>();
static int MAX = 30, r = 30;
public static void getcoordinates()
{
int x, y, count = 0;
Random r;
Point p;
while(count < MAX)
{
r = new Random();
x = r.nextInt(500) + 1;
y = r.nextInt(500) + 1;
p = new Point(x, y);
list.add(p);
count++;
}
while((--count) >= 0)
System.out.println((int)list.get(count).getX() + " " +
(int)list.get(count).getY());
}

static void getIntersectionPoints()


{
int i, j, count;
Point p1, p2;
Points i1, i2;
double R, l, h, x1, x2, y1, y2, d, m, xm, ym;
for(i = 0; i < MAX-1; i++)
{
p1 = list.get(i);
for(j = i+1; j < MAX; j++)
{
p2 = list.get(j);
//System.out.println(i + " " + j);
d = Math.sqrt(Math.pow(p2.getX() - p1.getX(), 2) +
Math.pow(p2.getY() - p1.getY(), 2));
if(d-r >= 0 && d-(2*r) <= 0)
{
xm = (p1.getX() + p2.getX()) / 2;
ym = (p1.getY() + p2.getY()) / 2;
m = (p2.getY() - p1.getY())/(p2.getX() - p1.getX());
//Slope
h = Math.sqrt((4*r*r) - (d*d))/2;
x1 = xm + (m * h)/Math.sqrt(1 + (m*m));
x2 = xm - (m * h)/Math.sqrt(1 + (m*m));
y1 = ym - h/Math.sqrt(1+(m*m));
y2 = ym + h/Math.sqrt(1+(m*m));
intersection.add(new Points((int)Math.ceil(x1)-1, (int)Math.ceil(y1)-1, (int)Math.ceil(x2)-
1, (int)Math.ceil(y2)-1));
}
}
}
System.out.println("INTERSECTION POINTS : ");
count = intersection.size();

while((--count) >= 0)
{
i1 = intersection.get(count);
System.out.println(i1.getX1() + " " + i1.getY1() + " " + i1.getX2() + " " +
i1.getY2());
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2D = (Graphics2D)g;
Point p;
Points p1;
int x, y;
g2D.drawLine(0, 0, 570, 0);
g2D.drawLine(570, 0, 565, 5);
g2D.drawLine(0, 0, 0, 565);
g2D.drawLine(0, 565, 5, 560);
//To display the points in the 2D-Plane
for(int i = 0; i < list.size(); i++)
{
p = list.get(i);
x = (int)p.getX();
y = (int)p.getY();
g2D.fillOval(x, y, 4, 4);
g2D.drawOval(x-r, y-r, 2*r, 2*r);
}
//To find the intersection of the circles
for(int i = 0; i < intersection.size(); i++)
{
p1 = intersection.get(i);
g2D.setColor(Color.RED);
g2D.fillOval(p1.getX1(), p1.getY1(), 3, 3);
g2D.fillOval(p1.getX2(), p1.getY2(), 3, 3);
g2D.drawLine(p1.getX1(), p1.getY1(), p1.getX2(), p1.getY2());
}
}
public static void main(String args[])
{
getcoordinates();
getIntersectionPoints();
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
JFrame f = new JFrame("POINTS PLOTTING");
f.getContentPane().add(new Positioning());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600, 600);
f.setVisible(true);
}
});
}
}

Points class
package positioning;
import java.awt.Point;

public class Points


{
int x1, y1, x2, y2;

Points() {}

Points(int x1, int y1,int x2, int y2)


{
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}

Points getPoint()
{
return this;
}

void setPoints(int x1, int y1,int x2, int y2)


{
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}

int getX1()
{
return this.x1;
}
int getX2()
{
return this.x2;
}
int getY1()
{
return this.y1;
}
int getY2()
{
return this.y2;
}
}

You might also like