2013-07-10 4 views
0

다각형의 영역을 계산하는 함수를 구현하려고합니다. 이 코드는 다각형을 그리는 것을 허용하지만 영역을 표시 할 때는 그 방법을 모르겠습니다. 몇 가지 방법으로 시도했지만 프로그래밍 초보자이기 때문에 도움을받을 수 있습니다. 여기에 코드입니다 : 당신이 교차에 의해 생성 된 모든 하위 다각형을 찾기 위해 교차로-COORD 측정기 및 다각형 가산기 알고리즘을 추가 할 수 있습니다다각형 그리기 및 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