Hi I have a geometry problem.Can anyone help with java code
Hi I have a non simple polygon . My job is to make it a simple. for that First I need to find the intersecting lines say line AB intersects line CD. If so then I must replace AB & CD with AC & BD or AD or BC which ever keeps the polygon closed . So can anyone help me out with this code
import java.io.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
public class Simple{
static int POINTWID = 4;// size of points
// the x and y arrays hold the coordinates
// the B array is the order of the points in the polygon
// You want to fill the C array with the simple polygon
static double x[] = new double[200];
static double y[] = new double[200];
static int B[] = new int[200]; // the permutation matrix
static int C[] = new int[200]; // the one that becomes simple
static SimpleFrame myFrame;
static int numPoints = 3;
public static void main(String args[]) {
makePolygons();
// Create the frame to draw on
myFrame = new SimpleFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(600, 600);
myFrame.setVisible(true);
}
public static void makePolygons(){
// Build an array of random points in the unit square
for(int i = 0; i < numPoints; i++){
x = Math.random();
y = Math.random();// Sample program
B = i;// default permutation
}
// Create the simple polygon
createSimplePolygon();
}
/*
* This is the only function you need to mess with
*/
public static void createSimplePolygon(){
// Initialize the C[] array with the identity permutation
for(int i = 0; i < numPoints; i++)
C = i;
// Bubble sort the points from left to right
for(int i = 0; i < numPoints; i++)
for(int j = 0; j < numPoints - 1; j++)
if(x[C[j]] > x[C[j+1]]){
int temp = C[j];
C[j] = C[j+1];
C[j+1] = temp;
}
}
public static class SimpleFrame extends JFrame{
public static JSlider numPointsSlider;
public SimpleFrame()
{
super("Create you own Simple Polygon");
Container content = getContentPane();
content.setLayout(new java.awt.BorderLayout());
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setPreferredSize (new java.awt.Dimension(300, 400));
tabbedPane.addTab("Scrambled", new ScrambledPanel());
tabbedPane.addTab("Simple", new SimplePanel());
content.add(tabbedPane, java.awt.BorderLayout.CENTER);
// Slider for the number of points
numPointsSlider = new JSlider (javax.swing.SwingConstants.HORIZONTAL,
3, 100, 11);
numPointsSlider.addChangeListener (new javax.swing.event.ChangeListener () {
public void stateChanged (javax.swing.event.ChangeEvent evt) {
numPointsSliderStateChanged (evt);
}
}
);
content.add(numPointsSlider, java.awt.BorderLayout.SOUTH);
}
private void numPointsSliderStateChanged (javax.swing.event.ChangeEvent evt) {
numPoints = numPointsSlider.getValue();
makePolygons();
repaint();
}
}
public static class ScrambledPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
// First set the scaling to fit the window
Dimension size = getSize();
int Xwid = (int) (0.95 * size.width);
int Ywid = (int) (0.95 * size.height);
// First draw the segments
g2.setColor(Color.red);
for(int i = 0; i < numPoints; i++)
g2.drawLine((int) (Xwid * x[B]),
(int) (Ywid * y[B]),
(int) (Xwid * x[B[(i+1) % numPoints]]),
(int)(Ywid * y[B[(i+1) % numPoints]]));
// Now draw the points
for(int i = 0; i < numPoints; i++){
g2.fillRect((int) (Xwid * x) - POINTWID,
(int) (Ywid * y) - POINTWID,
2*POINTWID + 1, 2*POINTWID + 1);
}
}
}
public static class SimplePanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
// First set the scaling to fit the window
Dimension size = getSize();
int Xwid = (int) (0.95 * size.width);
int Ywid = (int) (0.95 * size.height);
// First draw the segments
g2.setColor(Color.red);
for(int i = 0; i < numPoints; i++)
g2.drawLine((int) (Xwid * x[C]),
(int) (Ywid * y[C]),
(int) (Xwid * x[C[(i+1) % numPoints]]),
(int)(Ywid * y[C[(i+1) % numPoints]]));
// Now draw the points
for(int i = 0; i < numPoints; i++){
g2.fillRect((int) (Xwid * x) - POINTWID,
(int) (Ywid * y) - POINTWID,
2*POINTWID + 1, 2*POINTWID + 1);
}
}
}
}

