LoginSignup
1
2

More than 5 years have passed since last update.

Javaで回転行列を使った円の描画

Posted at

数学の延長で、回転行列の座標計算をプログラムでやってみました。

スクリーンショット 2015-11-27 12.05.48.png

コード内容

import javax.swing.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.geom.Line2D;
import java.awt.Color;

class Test2 extends JPanel{

    private static final int CENTERVALUE = 300; 


  public static void main(String[] args){
            JFrame frame = new JFrame();

            Test1 app = new Test1();
            frame.getContentPane().add(app);

            frame.setBackground(Color.BLACK);   // windowの背景色設定
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(false);
            frame.setBounds(0, 0, 600, 600);
            frame.setTitle("Test");
            frame.setVisible(true);
            Insets insets = frame.getInsets();  // 設定すべき値を求めるためのインスタンス化
            frame.setSize(600 + insets.left + insets.right,
                          600 + insets.top + insets.bottom); // 画面サイズ設定
          }

          public void paintComponent(Graphics g){
            Graphics2D g2 = (Graphics2D)g;
            g2.setColor(Color.blue);

            g2.draw(new Line2D.Double(300,   0, 300, 600));
            g2.draw(new Line2D.Double(  0, 300, 600, 300));

            g2.setColor(Color.green);
            drawTest1(g2);
          }

          public void drawTest1(Graphics2D g2){
              double x, y, xx, yy, r = 200, cx = 300, cy = 300;
              double[][] MathArray = new double[12][2];

              for(int i = 0; i < 360; i += 30){
                  /*  */
//                x = r * Math.sin(Math.toRadians(i)) + cx;
//                y = r * Math.cos(Math.toRadians(i)) + cy;
                  x = r * Math.cos(Radian(i));
                  y = r * Math.sin(Radian(i));
                  System.out.println(x - cx);
                  System.out.println(y - cy);
                  g2.draw(new Line2D.Double(x + cx, cy - y, cx + x, cy - y));
              }
          }
          public double Radian(double angle){
              angle = angle / 180 * 3.141592653589793;//Math.PI;
              return angle;
          }
}

開発環境
Mac Version 10.10.5, Eclipse Mars

Eclipseでimportして実行してみてください。

ターミナルで実行する場合は

javac Test2.java
java Test2

radian変換するという考えが思い浮かばず、これだけのプログラムに苦戦してしまいました...

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2