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);

}

}

}

}

[5019 byte] By [ncm0402a] at [2007-11-27 1:00:47]
# 1

> 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

In your example you have two lines (AB and CD). If they intersect, you end up with two lines again (AC and BD or AD and CB)! Where do the polygons come into play? Two lines are not a polygon.

It's a bit unclear to me what you want. Are you looking for the bounding box of a polygon? This is a square aligned with the x-, and y-axis which can be placed so that all the points from your polygon are covered by it.

Or are you perhaps looking for the convex hull of a polygon?

http://mathworld.wolfram.com/ConvexHull.html

Maybe the java.awt.Polygon class is of help to you:

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Polygon.html

prometheuzza at 2007-7-11 23:35:31 > top of Java-index,Other Topics,Java Game Development...
# 2
Maybe he means that if he has some Polygon ABCD, and two lines (like AB and CD) intersect, then he wants to fix it.A-B A-B \/ ||\ /||X >||/ \|| /\ ||C-D C-DBut I really don't follow him either.
CaptainMorgan08a at 2007-7-11 23:35:31 > top of Java-index,Other Topics,Java Game Development...
# 3
Ignore this message. On re-reading OP, what I posted was demonstrably false.
YAT_Archivista at 2007-7-11 23:35:31 > top of Java-index,Other Topics,Java Game Development...
# 4

Hi I am sorry I could explain you properly . Ok

My program gives me a polygon(as you can see when u run this program)But the polygon is a non simple polygon.So to make this polygon we must remove all the crossings betweeen edges in the polygon.The algorithm which i gave will remove all the crossings and make the polygon simple.SO my job is to take the existing code and implement the algorithm for this program in the Createsimpelpolygon() function. For this First the program must find whether two edges cross if they cross then swap the vertices like replace AB & CD with AC & BD or AD & BC.Which ever keeps the polygon closed . So as we go on we find the many crossings and iterate the algorithm on all the crossings until we get simple polygon.

ncm0402a at 2007-7-11 23:35:31 > top of Java-index,Other Topics,Java Game Development...
# 5

> Hi I am sorry I could explain you properly . Ok

> My program gives me a polygon(as you can see when u

> run this program)But the polygon is a non simple

> polygon.So to make this polygon we must remove all

> the crossings betweeen edges in the polygon.The

> algorithm which i gave will remove all the crossings

> and make the polygon simple.

You did not give an algorithm!

> SO my job is to take the

> existing code and implement the algorithm for this

> program in the Createsimpelpolygon() function. For

> this First the program must find whether two edges

> cross if they cross then swap the vertices like

> replace AB & CD with AC & BD or AD & BC.Which ever

> keeps the polygon closed .

Still not entirely clear to me. You cannot just go and replace vertexes from a polygon: that way you'll end up with a different polygon.

> So as we go on we find the

> many crossings and iterate the algorithm on all the

> crossings until we get simple polygon.

Like I said: I don't really understand what it is you're after. You did not respond to my suggestions, so I gather it is not what you're after?

What about Polygon Tessellation (Google for it)? Perhaps that's what you want.

Also, why do you not create a (or use java.awt's) Point class and a Polygon class which holds a java.util.Set of Point's? Your current code looks rather messy.

prometheuzza at 2007-7-11 23:35:31 > top of Java-index,Other Topics,Java Game Development...