2013-05-27 8 views
6

मेरे पास एक आयताकार है जो इसके मध्य के चारों ओर घूमता है और मेरे पास एक और आयताकार है जिसे मैं घूर्णन आयत के ऊपरी दाएं कोने से जोड़ना चाहता हूं। समस्या यह है कि मुझे नहीं पता कि कोने को कैसे प्राप्त किया जाए ताकि दूसरा आयत हमेशा उस कोने पर फंस जाएगा।घूर्णन आयत के कोनों को प्राप्त करें

यह मेरा नमूना कोड है। अभी दूसरा आयत एक ही स्थान पर उसी स्थान पर होगा जो परिणाम नहीं है जिसके बाद मैं हूं।

package Test; 

import java.awt.*; 
import java.awt.event.*; 
import java.awt.geom.*; 
import javax.swing.*; 

class Test{ 

    public static void main(String[] args){ 
     new Test(); 
    } 

    public Test(){ 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       JFrame frame = new JFrame("Test"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new Graphic()); 
       frame.setSize(1000,700); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 

class Graphic extends JPanel{ 
    private int x, y, windowW, windowH; 
    private double angle; 
    private Rectangle rect1, rect2; 
    private Path2D path; 
    private Timer timer; 
    private AffineTransform rotation; 

    public Graphic(){ 
     windowW = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth(); 
     windowH = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight(); 
     path = new Path2D.Double(); 
     rotation = new AffineTransform(); 
     angle = 0; 
     x = windowW/2; 
     y = windowH/2; 
     timer = new Timer(100, new ActionListener(){ 
      @Override 
      public void actionPerformed(ActionEvent e){ 
       angle += .1; 
       if(angle > 360) angle -= 360; 
       repaint(); 
      } 
     }); 
     timer.start(); 
    } 

    @Override 
    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     Graphics2D g2d = (Graphics2D) g; 
     rotation.setToTranslation(500, 200); 
     rotation.rotate(angle, 32, 32); 
     rect1 = new Rectangle(0, 0, 64, 64); 
     path = new Path2D.Double(rect1, rotation); 
     rect2 = new Rectangle(path.getBounds().x, path.getBounds().y, 10, 50); 
     g2d.fill(path); 
     g2d.fill(rect2); 
    } 
} 
+0

बाध्य आप 'angle' आप डिग्री में यह व्यक्त करना चाहता हूँ सुझाव है कि लगता है पर डाल; फिर भी 'घुमावदार' फ़ंक्शन रेडियंस में अपना पहला पैरामीटर लेता है। स्थिति 'अगर (कोण> 360) कोण - = 360' वास्तव में एक अजीब कूद का कारण बनता है जब यह पहुंचा जाता है, और आयताकार बहुत तेज़ घूम रहा है जितना आप चाहते थे। – Arend

उत्तर

4

गणितीय समाधान :)

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    Graphics2D g2d = (Graphics2D) g; 
    rotation.setToTranslation(500, 200); 
    rotation.rotate(angle, 32, 32); 
    rect1 = new Rectangle(0, 0, 64, 64); 
    path = new Path2D.Double(rect1, rotation); 
    double r = 32.0 * Math.sqrt(2); 
    // (532, 232) - coordinates of rectangle center  | 
    // you can change position of second rectangle by this V substraction (all you need to know is that the full circle corresponds to 2Pi) 
    int x2 = (int) Math.round(532 + r * Math.cos(angle - Math.PI/4)); 
    int y2 = (int) Math.round(232 + r * Math.sin(angle - Math.PI/4)); 
    rect2 = new Rectangle(x2, y2, 10, 50); 
    g2d.fill(path); 
    g2d.fill(rect2); 
} 
बेशक

, कुछ स्थिरांक वर्ग क्षेत्रों, नहीं विधि चरों होना चाहिए।

2

मुझे यकीन है कि हो सकता है के लिए इस कोड का परीक्षण नहीं कर सकता लेकिन मेरा मानना ​​है कि यह उचित कार्य कोड है कि आप चाहते हैं

int hw = -width/2; 
int hh = -height/2; 
int cos = Math.cos(theta); 
int sin = Math.sin(theta); 
int x = hw * cos - hh * sin; 
int y = hw * sin + hh * cos; 

यह आपको थीटा के आधार पर शीर्ष बाएं कोने मिल जाएगा, रोटेशन, की वर्ग। अन्य कोनों प्राप्त करने के लिए आप सिर्फ hw और hh मूल्यों को बदल का उपयोग करें:

//top right corner 
hw = width/2 
hh = -height/2 

//bottom right corner 
hw = width/2 
hh = height/2 

//bottom left corer 
hw = -width/2 
hh = height/2 

मुझे आशा है कि इस मदद करता है

संबंधित मुद्दे