Link to home
Start Free TrialLog in
Avatar of michael306
michael306

asked on

Computer Vision

hey, can any one tell me on how to Write a program to plot nested triangles using the following steps.

Step 1. Plot a triangle with vertices .
(-20,-20),(0,20) and (20,-20)
 

Step 2. Apply the following transformation.

 T=0.9  0  0                    cos20 sin20 0
   0  0.9  0             And  -sin20 cos20  0
   0  0    1                      0   0     1

are the matrices and how to do it

 

Step 3. Repeat Step 2 eighteen times, using the transformed coordinates as the input


 
, well i was working as a hobby project and interested to implement in th e rectangle and others , but at first on how to do it in the triangle ones
Thanks
Avatar of superschlonz
superschlonz

Here is a little class which you can use as application or as applet.


import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Frame;
import java.awt.Button;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class triangle extends Applet
{
     double matrix[] = new double[9];
     double points[][] = new double[][] {
          new double[] { -100.0, -100.0 },
          new double[] { 0.0, 100.0 },
          new double[] { 100.0, -100.0 }
     };
     int offsetx = 150;
     int offsety = 150;
     public void init()
     {
          for( int i=0; i<9; ++i )
          {
               matrix[i] = (i%4)==0?1.0:0.0;
          }
          scale( matrix, 0.9 );
          rotate( matrix, 20.0 );
          //setSize( 300, 300 );
     }
     public void paint( Graphics g )
     {
          double p[][] = new double[3][];
          int x[] = new int[3];
          int y[] = new int[3];
          for( int j=0; j<3; ++j )
          {
               p[j] = (double[])points[j].clone();
               x[j] = (int)p[j][0] + offsetx;
               y[j] = (int)p[j][1] + offsety;
          }
          g.drawPolygon( x, y, 3 );
          for( int i=0; i<20; ++i )
          {
               for( int j=0; j<3; ++j )
               {
                    transform( matrix, p[j] );
                    x[j] = (int)p[j][0] + offsetx;
                    y[j] = (int)p[j][1] + offsety;
               }
               g.drawPolygon( x, y, 3 );
          }
     }
     public void scale( double m[], double f )
     {
          m[0] *= f;
          m[1] *= f;
          m[3] *= f;
          m[4] *= f;
     }
     public void rotate( double m[], double a )
     {
          double r = a/180.0*Math.PI;
          double m2[] = new double[9];
          double m3[] = (double[])m.clone();
          m2[0] = Math.cos(r);
          m2[1] = Math.sin(r);
          m2[3] = -Math.sin(r);
          m2[4] = Math.cos(r);
          m2[2] = m2[5] = m2[6] = m2[7] = 0.0;
          m2[8] = 1.0;
          for( int i=0; i<3; ++i )
          {
               for( int j=0; j<3; ++j )
               {
                    m[i*3+j] = 0;
                    for( int k=0; k<3; ++k )
                    {
                         m[i*3+j] += m2[i*3+k] * m3[j+k*3];
                    }
               }
          }
     }
     public void transform( double m[], double v[] )
     {
          double x=v[0],y=v[1];
          v[0] = x*m[0] + y*m[1] + m[2];
          v[1] = x*m[3] + y*m[4] + m[5];
     }
     public static void main( String args[] )
     {
          Frame frame = new Frame( "triangle" );
          frame.setLayout( new BorderLayout() );
          triangle t = new triangle();
          t.init();
          frame.add( t, BorderLayout.CENTER );
          Button b = new Button( "exit" );
          b.addActionListener( new ActionListener(){
               public void actionPerformed(ActionEvent e) {
                    System.exit(0);
               }
          });
          frame.add( b, BorderLayout.SOUTH );
          frame.setSize( 300, 340 );
          frame.setVisible( true );
     }
}

I hope you like it.
ASKER CERTIFIED SOLUTION
Avatar of kylar
kylar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of michael306

ASKER

Well, i think this really served my purpose to start off,
The calculation of mtx one was really interesting as i did not knew that one could do on a applet.
?? that shouldn't have happened.

Kylar
To whom should this award have been given?

Moondancer
Community Support Moderator @ Experts Exchange
Assuming this award was intended for superschlonz, I'll need to create a new question to flow these point awards, and refund the points granted within this question.

Moondancer
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=java&qid=20137612

Points for superschlonz

Moondancer
Community Support Moderator @ Experts Exchange
well the award was for superschlonz, and i appreciate for your interest for looking into that, and awarding it to the correct expert.