2013-07-10 138 views
0

我試圖實現一個函數來計算多邊形的面積。這段代碼允許你繪製一個多邊形,但是當涉及到顯示區域時,我不知道該怎麼做。我嘗試了幾種方法,但我仍然是編程初學者,所以我會很感激任何幫助。這裏是代碼:在Java中繪製多邊形並計算面積

import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.event.*; 
import javax.swing.*; 
import java.awt.Color; 
import java.awt.Polygon; 

public class DrawPolygons extends JApplet implements ActionListener, MouseListener 
{ 
private static final int NUMPOINTS = 500; //Up to 500 points can be chosen 

private JButton finish;     //Button to indicate user is done entering  points 
private Polygon shape;     //polygon object to be drawn 
private boolean isDrawn;     //boolean flag for when the user is finished drawing 
private int count;      //how many points the user has clicked 
private Color color;      //color of the polygon after user finalizes points 
private int[] x;       //x coordinates of each point user picks 
private int[] y;       //y coordinates of each point user picks 
private float sum; 
private double area; 



public void init()      //set up GUI 
{ 
    setLayout(new FlowLayout()); 

    addMouseListener(this);    //adds MouseListener for mouse clicks 

    isDrawn = false;      //isDrawn is initially false 
    count = 0;       //count starts as 0 
    x = new int[NUMPOINTS];    //allows for up to 500 points to be chosen 
    y = new int[NUMPOINTS];    //allows for up to 500 points to be chosen 

    finish = new JButton("Finalize points"); //creates finish button 
    finish.addActionListener(this);    //adds finish button to ActionListener 
    add(finish);        //adds finish button to GUI 

    color = Color.BLACK;    //color is intially black, and will remain 
             // so if user cancels the color chooser 

    //JOptionPane.showMessageDialog(null, "Click points that will make up the polygon. After each" +  
    //         "point is entered, press the Finalize Points button"); 
    shape = new Polygon();    //creates the Polygon shape 



} 



public void paint(Graphics g)   //draws the Polylines, Polygons, and sets the color 
{    
    super.paint(g); 

    g.drawPolyline(x, y, count);  //draws the polyline specified by user 
             // mouseclick 
    g.setColor(color);     //sets the color to the user chosen color 

    if(isDrawn)       //if finalize button is clicked 
    { 

     g.fillPolygon(x, y, count);  //finalizes polylines into 


    }         // polygon and fills shape 
} 

    public void actionPerformed(ActionEvent a) //decides what to do when finalize button is pressed 
{ 
    if(a.getSource() == finish)    //if the finalize button is pressed 
    { 
     isDrawn = true;      //isDrawn is set to true, ending the users ability 
              // to add more points, and fills the polygon 
     color = Color.red; 


     //JColorChooser.showDialog(this, "Choose a color", color); //color is set to users choice 
     repaint();  

     JOptionPane.showMessageDialog(null, "The area is "); 
    } 
} 

public void mouseClicked(MouseEvent e)  //save coordinates of clicks 
{ 


    if(isDrawn == false && count < NUMPOINTS) //if the finalize button is not pressed 
    {           // the user can add additional points 
     x[count] = e.getX();     //adds the x point at the current mouse x coordinate 
     y[count] = e.getY();     //adds the y point at the current mouse y coordinate 
     count++;        //count increases with each mouse click 
     repaint(); 

    } 

    else if (e.isShiftDown()) { 
      // Clear the applet. (This only requires a repaint.) 
      // Also, set count to zero to start a new polygon. 
      count = 0; 
      isDrawn = false; 
      repaint(); 
        } 


    } 

    private float getPolygonArea(int[] x, int[] y, int count) 
    { 
     float sum_but_no_result=0; 

    for(int i=0;i<(count-1);i++)  // count is point number of polygon 
     { 
     sum_but_no_result+=x[i]*y[i+1] + y[i]*x[i+1]; 
     } 
     sum_but_no_result+=x[count-1]*y[0] + y[count-1]*x[0]; 

     float sum = (float)Math.abs(sum_but_no_result)/2.0f; 
    return sum; 
    } 


//Empty Implementation provided here so we can implement MouseListener (needed because we must 
// provide concrete forms of all methods of an interface to implement it 

    public void mousePressed(MouseEvent e){}; 
    public void mouseReleased(MouseEvent e){}; 
    public void mouseEntered(MouseEvent e){}; 
    public void mouseExited(MouseEvent e){};  

    } 

回答

0
float x[N],y[N]; // point coordinates as x1,y1 x2,y2 .... 
... 
    ... 
    ... 
... 
float sum_but_no_result=0; 

for(int i=0;i<(N-1);i++) // N is point number of polygon 
{ 
     sum_but_no_result+=x[i]*y[i+1] + y[i]*x[i+1]; 
} 
sum_but_no_result+=x[N-1]*y[0] + y[N-1]*x[0]; 

float sum= (float)Math.abs(sum_but_no_result)/2.0f; 

http://www.mathopenref.com/coordpolygonarea.html

對於自相交多邊形,您可以添加一個交叉點座標取景器和多邊形的加法器算法找到相交產生的所有子多邊形,遞歸。

+0

我在使用下面的代碼中的公式時遇到了問題 – user2569810

+0

有什麼樣的麻煩? –

+0

我編輯了上面的代碼,我不知道如何實現它... – user2569810