Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. package edu.blaky.ship;
  2.  
  3. public class Ship {
  4.  
  5.     private double positionX;
  6.     private double positionY;
  7.     private double direction;
  8.  
  9.     public Ship(double positionX, double positionY, double direction) {
  10.         this.direction = direction;
  11.         this.positionX = positionX;
  12.         this.positionY = positionY;
  13.  
  14.     }
  15.    
  16.     private double calculateXAxisDistance(double distance){
  17.         return (distance * Math.sin(getRadFromDegrees(90 - this.direction))) / Math.sin(getRadFromDegrees(90));
  18.     }
  19.     private double calculateYAxisDistance(double distance){
  20.         return (distance * Math.sin(getRadFromDegrees(this.direction))) / Math.sin(getRadFromDegrees(90));
  21.     }
  22.  
  23.     private double getRadFromDegrees(double degrees) {
  24.         return degrees * (Math.PI / 180);
  25.     }
  26.  
  27.     public double getPositionX() {
  28.         return positionX;
  29.     }
  30.  
  31.     public double getPositionY() {
  32.         return positionY;
  33.     }
  34.  
  35.     public double getDirection() {
  36.         return direction;
  37.     }
  38.  
  39.     public void turn(double degree) {
  40.         this.direction = (this.direction + degree) % 360;
  41.     }
  42.  
  43.     public void move(double distance) {
  44.         this.positionX += calculateXAxisDistance(distance);
  45.         this.positionY += calculateYAxisDistance(distance);
  46.     }
  47.    
  48.     public void printStatus(){
  49.         System.out.println("This ship is at X:" + positionX +" Y:" + positionY + " and facing " + direction + "*.");
  50.     }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement