The Java judgment Circle intersects the rectangle

Source: Internet
Author: User

public class Circlerectangleintersect {private StringBuffer ntersectinformation = new StringBuffer (), Public Stringbuffe R Getntersectinformation () {return ntersectinformation.} public void Setntersectinformation (StringBuffer ntersectinformation) {this.ntersectinformation = ntersectinformation;/** * @param x * Center x * @param y * Center y * @param X * Rectangular Center x * @param y * Rectangular Center y * @param c * Rectangle side * @param k * Rectangle on the other side * @param R * Circle radius void 2010-6-18 Author Mayankai *//Up/Down Left and right Quad 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 ("in line with the top and bottom four sides"); Ntersectinformation.append ("Meet the top and bottom sides,"); return true;} else {System.out.println ("not in line with the top and bottom four sides"); Ntersectinformation.append ("not meet up and down the four sides,"); return false; }///Top Left public boolean ZSJ (double x, double y, double X, double, 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 ("match upper left Corner"); Ntersectinformation.append ("Match upper left Corner,"); return true;} else {System.out.pri Ntln ("does not conform to the upper left Corner"); Ntersectinformation.append ("does not conform to the upper left corner,"); return false; }///upper right corner public boolean YSJ (double x, double y, double X, double, 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 ("conforming to upper right corner"); Ntersectinformation.append ("conforming to upper right corner,"); return true;} else {System.out.pri NTLN ("Not conforming to the upper right Corner"); Ntersectinformation.append ("Not conforming to the upper right corner,"); return false; }///lower left corner 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 && Amp Y <= y + c/2 + R) && zxjl (x, Y, x, Y, C, K, R)) {System.out.println ("conform to lower left corner"); Ntersectinformation.append ("compliant Lower left corner, "); return true; else {System.out.println ("does not conform to the bottom left corner"); Ntersectinformation.append ("not conforming to the lower left corner,"); return false;}} Lower right Corner public boolean YXJ (double x, double y, double x, double y, double C, double K, double R) {if (x >= x + K/2 & amp;& 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 ("conforming to lower right corner"); Ntersectinformation.append ("conforming to lower right corner,"); return true;} else {System.out.println ("Not conforming to the lower right corner"); Ntersectinformation.append ("Not conforming to the lower right corner,"); return false; }//center and Rectangle Diagonal intersection point (Central distance) public boolean ZXJL (double x, double y, double x, double y, double C, double K, double R) {Doubl E ZX = x-k/2;//left x Double yx = x + k/2;//right x double sy = y-c/2;//up Y double xy = y + c/2;//y System.out.println ("Upper left Corner:" + ZX + ":" + sy); System.out.println ("Bottom left corner:" + ZX + ":" + xy); SYSTEM.OUT.PRINTLN ("upper right Corner:" + Yx + ":" + sy); System.out.println ("Bottom right corner:" + 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 ("Fit Center Distance"); Ntersectinformation.append ("Fit Center distance,"); return true;} else {System.out.println ("Not in line with center distance"); Ntersectinformation.append ("Not in line with center distance,"); return false;}} Whether to intersect public boolean isintersect (double x, double y, double X, double, 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 ("intersect"); Ntersectinformation.append ("intersect,"); return true;} else {System.out.println ("disjoint"); Ntersectinformation.append ("disjoint,"); 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;//circle radius int c = 120; Rectangle y-direction edge int k = 70;//Rectangle x-directional side//circle upper left corner int x = 180; int y = 230; The upper-left corner of the rectangle int X = 86; int Y = 138; Draw round G.drawoval (x, Y, r*2, r*2); Draw rectangular G.drawrect (X, Y, K, c); G.setcolor (COLOR.BLACK); Output Circle and Rectangular Center System.out.println ("x:" + (X+r) + "Y:" + (Y+R) + "x:" + (X+K/2) + "Y:" + (Y+C/2) + "C:" +c+ "K:" +k+ "R:" +r); Determine whether to intersect 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);//Storage mouse X,y coordinates final JTextField jfieldx = new JTextField (); Final JTextField Jfieldy = new JTextField (); X,y label JLabel jlabelx = new JLabel ("x:"); JLabel jlabely = new JLabel ("Y:"); Store intersect information JTextArea JTextArea = new JTextArea (); Paintovalpane paintovalpane= new Paintovalpane (); Add Mouse Move Information Addmousemotionlistener (new Mousemotionlistener () {public void mousedragged (MouseEvent e) {} public void mouse Moved (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 ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.