Advertisement
Guest User

MGP Assignment 1

a guest
Dec 7th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1. // Vector2D class
  2. public class Vector2D {
  3.     double x, y;
  4.     Vector2D(){
  5.         x = y = 0;
  6.     };
  7.     Vector2D(double _x, double _y){
  8.         x = _x;
  9.         y = _y;
  10.     };
  11.     double Length(){
  12.         return Math.sqrt(x*x + y*y);
  13.     };
  14.     Vector2D add(Vector2D v) {
  15.         return new Vector2D(x + v.x, y + v.y);
  16.     };
  17.     Vector2D minus(Vector2D v) {
  18.         return new Vector2D(x - v.x, y - v.y);
  19.     };
  20.     void normalize(){
  21.         double len = Length();
  22.         if( len != 0 )
  23.         {
  24.             x /= len;
  25.             y /= len;
  26.         }
  27.     };
  28.     void setLength(double value)
  29.     {
  30.         normalize();
  31.         x *= value;
  32.         y *= value;
  33.     };
  34.     void scale(double f)
  35.     {
  36.         double x2 = x;
  37.         double y2 = y;
  38.         normalize();
  39.         x = x2*f;
  40.         y = y2*f;
  41.  
  42.     };
  43.     void rotate(double theta, double cx, double cy)
  44.     {
  45.         double sin = Math.sin(theta);
  46.         double cos = Math.cos(theta);
  47.         double dx = x-cx;
  48.         double dy = y-cy;
  49.         x = cx+(dx)*cos-(dy)*sin;
  50.         y = cy+(dy)*cos+(dx)*sin;
  51.     };
  52.     Vector2D normal()
  53.     {
  54.         return new Vector2D(x,-y);
  55.     };
  56.  
  57.     // the dot product implemented as class function
  58.     static double dot(Vector2D v1, Vector2D v2)
  59.     {
  60.         return v1.x*v2.x+v1.y*v2.y;
  61.     };
  62.  
  63. };
  64.  
  65.  
  66. // Player class
  67. public class Player{
  68.     // variables
  69.     //What i did was to create a private variable from the class above ^
  70.     private Vector2D playerPosition;
  71.  
  72.     public Vector2D getPlayerPosition(){
  73.         return playerPosition;
  74.     };
  75. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement