public class CircleRectangleIntersect { private StringBuffer ntersectInformation = new StringBuffer(); public StringBuffer getNtersectInformation() { return ntersectInformation; } public void setNtersectInformation(StringBuffer ntersectInformation) { this.ntersectInformation = ntersectInformation; } /** * * @param x * 圓心x * @param y * 圓心y * @param X * 矩形中心X * @param Y * 矩形中心Y * @param c * 矩形一邊 * @param k * 矩形另一邊 * @param r * 圓半徑 void 2010-6-18 author mayankai */ // 上下左右四邊 public boolean sxzy(double x, double y, double X, double Y, double c, double k, double r) { double xX = x - X; double yY = y - Y; if (((Math.abs(xX) <= k / 2 + r) && (Math.abs(yY) <= c / 2)) || (Math.abs(xX) <= k / 2 && (y >= Y + c / 2 && y <= Y + c / 2 + r)) || (Math.abs(xX) <= k / 2 && (y >= Y - c / 2 - r && y <= Y - c / 2))) { System.out.println("符合上下左右四邊"); ntersectInformation.append("符合上下左右四邊,"); return true; } else { System.out.println("不符合上下左右四邊"); ntersectInformation.append("不符合上下左右四邊,"); return false; } } // 左上方 public boolean zsj(double x, double y, double X, double Y, double c, double k, double r) { if ((x >= X - k / 2 - r && x <= X - k / 2) && (y >= Y - c / 2 - r && y <= Y - c / 2) && zxjl(x, y, X, Y, c, k, r)) { System.out.println("符合左上方"); ntersectInformation.append("符合左上方,"); return true; } else { System.out.println("不符合左上方"); ntersectInformation.append("不符合左上方,"); return false; } } // 右上方 public boolean ysj(double x, double y, double X, double Y, double c, double k, double r) { if ((x >= X + k / 2 && x <= X + k / 2 + r) && (y >= Y - c / 2 - r && y <= Y - c / 2) && zxjl(x, y, X, Y, c, k, r)) { System.out.println("符合右上方"); ntersectInformation.append("符合右上方,"); return true; } else { System.out.println("不符合右上方"); ntersectInformation.append("不符合右上方,"); return false; } } // 左下角 public boolean zxj(double x, double y, double X, double Y, double c, double k, double r) { if ((x >= X - k / 2 - r && x <= X - k / 2) && (y >= Y + c / 2 && y <= Y + c / 2 + r) && zxjl(x, y, X, Y, c, k, r)) { System.out.println("符合左下角"); ntersectInformation.append("符合左下角,"); return true; } else { System.out.println("不符合左下角"); ntersectInformation.append("不符合左下角,"); return false; } } // 右下角 public boolean yxj(double x, double y, double X, double Y, double c, double k, double r) { if ((x >= X + k / 2 && x <= X + k / 2 + r) && (y >= Y + c / 2 && y <= Y + c / 2 + r) && zxjl(x, y, X, Y, c, k, r)) { System.out.println("符合右下角"); ntersectInformation.append("符合右下角,"); return true; } else { System.out.println("不符合右下角"); ntersectInformation.append("不符合右下角,"); return false; } } // 圓心和矩形對角線相交點(中心距離) public boolean zxjl(double x, double y, double X, double Y, double c, double k, double r) { double zx = X - k / 2;// 左x double yx = X + k / 2;// 右x double sy = Y - c / 2;// 上y double xy = Y + c / 2;// 下y System.out.println("左上方:" + zx + ":" + sy); System.out.println("左下角:" + zx + ":" + xy); System.out.println("右上方:" + yx + ":" + sy); System.out.println("右下角:" + yx + ":" + xy); if ((Math.pow((x - zx), 2) + Math.pow((y - sy), 2) <= Math.pow(r, 2)) || (Math.pow((x - yx), 2) + Math.pow((y - sy), 2) <= Math.pow( r, 2)) || (Math.pow((x - zx), 2) + Math.pow((y - xy), 2) <= Math.pow( r, 2)) || (Math.pow((x - yx), 2) + Math.pow((y - xy), 2) <= Math.pow( r, 2))) { System.out.println("符合中心距"); ntersectInformation.append("符合中心距,"); return true; } else { System.out.println("不符合中心距"); ntersectInformation.append("不符合中心距,"); return false; } } // 是否相交 public boolean isIntersect(double x, double y, double X, double Y, double c, double k, double r) { if (sxzy(x, y, X, Y, c, k, r) || zsj(x, y, X, Y, c, k, r) || ysj(x, y, X, Y, c, k, r) || zxj(x, y, X, Y, c, k, r) || yxj(x, y, X, Y, c, k, r)) { System.out.println("相交"); ntersectInformation.append("相交,"); return true; } else { System.out.println("不相交"); ntersectInformation.append("不相交,"); return false; } } public static void main(String[] args) { // System.out.println(new CircleRectangleIntersect().isIntersect(1, 2, // 3, // 4, 5, 6, 7)); // System.out.println(Math.pow(Math.sqrt(1250) + 70, 2)); CircleRectangleIntersect cri = new CircleRectangleIntersect(); cri.isIntersect(1, 2, 3, 4, 5, 6, 7); System.out.println(cri.getNtersectInformation()); } }
import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; class PaintovalPane extends JPanel { private CircleRectangleIntersect cri = new CircleRectangleIntersect(); private String information ; public CircleRectangleIntersect getCri() { return cri; } public void setCri(CircleRectangleIntersect cri) { this.cri = cri; } public String getInformation() { return information; } public void setInformation(String information) { this.information = information; } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.gray); int r = 70;// 圓半徑 int c = 120;// 矩形y方向的邊 int k = 70;// 矩形x方向的邊 //圓左上方點 int x = 180 ; int y = 230; //矩形左上方點 int X = 86; int Y = 138; //畫圓 g.drawOval(x, y, r*2, r*2); //畫矩形 g.drawRect(X, Y, k, c); g.setColor(Color.black); //輸出圓和矩形的中心 System.out.println("x:"+(x+r)+"y:"+(y+r)+"X:"+(X+k/2)+"Y:"+(Y+c/2)+"c:"+c+"k:"+k+"r:"+r); //判斷是否相交 System.out.println(cri.isIntersect(x+r, y+r, X+k/2, Y+c/2, c, k, r)); System.out.println(cri.getNtersectInformation()); setInformation(cri.getNtersectInformation().toString()); } } class PaintovalFrame extends JFrame { public PaintovalFrame() { setTitle("lamp"); setSize(800, 800); //存放滑鼠x,y座標 final JTextField jFieldx = new JTextField(); final JTextField jFieldy = new JTextField(); //x,y標籤 JLabel jLabelx = new JLabel("x:"); JLabel jLabely = new JLabel("y:"); //存放相交資訊 JTextArea jTextArea = new JTextArea(); PaintovalPane paintovalPane= new PaintovalPane(); //添加滑鼠移動資訊 addMouseMotionListener(new MouseMotionListener(){ public void mouseDragged(MouseEvent e) { } public void mouseMoved(MouseEvent e) { int x = e.getX(); int y = e.getY(); jFieldx.setText(x+""); jFieldy.setText(y+""); } }); Container contentPane = getContentPane(); contentPane.setLayout(null); contentPane.add(paintovalPane); paintovalPane.setBounds(0, 0, 500, 500); contentPane.add(jFieldx); contentPane.add(jFieldy); contentPane.add(jLabelx); contentPane.add(jLabely); jTextArea.setText(paintovalPane.getInformation()); contentPane.add(jTextArea); jFieldx.setBounds(600,600,50,20); jFieldy.setBounds(600,630,50,20); jLabelx.setBounds(550,600,20,20); jLabely.setBounds(550,630,20,20); jTextArea.setBounds(100,600,200,100); // contentPane.add(jFieldx,BorderLayout.SOUTH); // contentPane.add(jFieldy,BorderLayout.EAST); setDefaultCloseOperation(EXIT_ON_CLOSE); } } public class Paintoval { public static void main(String[] args) { JFrame f = new PaintovalFrame(); f.show(); } }