Sie sind auf Seite 1von 20

Appendix A Java Code

Code for Trajectory Using Law of Sines and Law of Cosines and Pi Space Examples
package com.physics.math; import java.util.HashMap; /* * * * * Test program to demonstrate the Pi-Space Theory Trajectory. * * Author : Martin Brady * * 'ps' methods indicate Pi-Space * * * */ public class PiSpaceTrajectory extends PiSpaceExamples { public PiSpaceTrajectory(int unit) { super(unit); } /* * * return a distance * */ public double lawOfCosines(double a, double b, double angle) { double rangle = convertDegreesToRadians(angle); double d = Math.sqrt(Math.pow(a,2) + Math.pow((b),2) 2*a*b*Math.cos(rangle)); System.out.println("lawOfCosines (a/b/angle/radians/cos) "+a+"/"+b+"/"+angle+"/"+rangle+"/"+Math.cos(rangle)+" Value is "+d); return Math.sqrt(Math.pow(a,2) + Math.pow((b),2) 2*a*b*Math.cos(rangle)); } /* * * return an angle * */

public double lawOfSinesAngle(double a, double aAngle, double b) { double rangle = convertDegreesToRadians(aAngle); System.out.println("lawOfSinesAngle (a/aAngle/b/radians/sin/angle) "+a+"/"+aAngle+"/"+b+"/"+rangle+"/"+Math.sin(rangle)+"/"+convertRadians ToDegrees(Math.asin((Math.sin(rangle)/a)*b))); return convertRadiansToDegrees(Math.asin((Math.sin(rangle)/a)*b)) ; } public static double convertDegreesToRadians (double degrees) { double radians = (Math.PI / 180) * degrees; return (radians); } public { double degrees = (180 / Math.PI) * radians; return (degrees); } double convertRadiansToDegrees(double radians)

/* * * TODO : return a length * */ public double lawOfSinesLength(double a, double aAngle, double bAngle) { return 0.0;//TODO }

/* * * Take the last position and apply it to the next one * */ public HashMap calculateNextTrajectoryPosition(HashMap vals) { HashMap hm = new HashMap(); double angleAxesOffset = (Double) vals.get("angleAxesOffset"); double x1 = (Double) vals.get("x1"); double y1 = (Double) vals.get("y1"); double x1COG = (Double) vals.get("x1COG"); double y1COG = (Double) vals.get("y1COG"); double radius = (Double) vals.get("radius");

double distanceFromCOGKilometers = (Double) vals.get("distanceFromCOG"); double fieldAccelerationKMPerSecond = (Double) vals.get("fieldAcceleration"); double velocityKiloMetresPerSecond = (Double) vals.get("velocity"); double angleWRTGravity = (Double) vals.get("angleWRTGravity"); double miliseconds = (Double) vals.get("miliseconds"); hm = calculateNextTrajectoryPosition( angleAxesOffset, x1, y1, x1COG, y1COG, fieldAccelerationKMPerSecond, velocityKiloMetresPerSecond, angleWRTGravity, miliseconds,radius); return hm; }

/* * * get distance between points (x1,y1) (x2,y2) * */ public double getDistance(double x1, double y1, double x2, double y2) { double xlen = x2 - x1; double ylen = y2 - y1; return Math.sqrt(Math.pow(xlen,2) + Math.pow(ylen,2)); } /* * * Calculate Gravity * */ public double calcGravity(double mass1, double mass2, double distance) { double gravity = Math.pow(distance,2); (ugc() * mass1 * mass2) /

System.out.println("(mass1/mass2/distance/gravity) "+mass1+"/"+mass2+"/"+distance+"/"+gravity); return gravity; }

/* * *

* position x1, y1 * cog x1,y1 * velocity of object kmps * angle movement wrt to gravity * field acceleration KPMS (e.g. Gravity Field) * * */ public HashMap calculateNextTrajectoryPosition( double angleAxesOffset, double x1Obj, double y1Obj, double x1COG, double y1COG, double fieldAcceleration, double velocity, double angleWRTGravity, double miliseconds, double radius) { System.out.println("------------------------------------------------------------------------------------------- ITERATION START "); HashMap h = new HashMap(); double time = miliseconds / 1000; //Calculate COG distance using COG x1,y1 and satellite x1,y1 double distanceFromCOGKilometers = getDistance(x1Obj, y1Obj, x1COG, y1COG); h.put("angleWRTGravity", angleWRTGravity); h.put("miliseconds", miliseconds); h.put("fieldAcceleration", fieldAcceleration); h.put("x1COG", x1COG); h.put("y1COG", y1COG); h.put("radius", radius); System.out.println("velocity is "+velocity); System.out.println("angleAxesOffset is "+angleAxesOffset); System.out.println("distanceFromCOG is "+distanceFromCOGKilometers); System.out.println("angleWRTGravity is "+angleWRTGravity); double vt =velocity * time; System.out.println("vt is "+vt); //To achieve an ellipse, we need to use the GMm/r^2! double mass1 = 1; //simulate mass for now, knowing the fieldAcceleration and the radius (assumed distanceFromCOG for now...) //TODO - Later pass this in. User can choose. //planet radius double planetRadius = radius; //m = a*r^2/G double mass2 = Math.pow(planetRadius, 2)*fieldAcceleration/ugc(); // this is more of a hack but it works for now... //we imagine a planet with mass2 and radius planetRadius;

double grav = calcGravity(mass1,mass2,distanceFromCOGKilometers); //grav = 5; double accDist = 0.5*grav*Math.pow(time,2); //TODO : we can use the Pi-Space formula here later... for strong Grav System.out.println("acc dist is " +accDist); //**** //**** Step 1: Calculate distance u and new angle WRT gravity Alpha //**** System.out.println(""); System.out.println("Step 1: Calculate distance u and new angle WRT gravity Alpha"); //First calculate u = new velocity per second //Rule of thumb: Extract the field first, then apply the angle WRT to gravity, produces 180-angleWRTGravity //Reason to do it this way, we get acceleration in the direction of movement towards COG //while using the Law of the Cosines double interiorAngle = 180 - angleWRTGravity; double u = lawOfCosines(vt,accDist,interiorAngle); System.out.println("distance u which is distance between lengths "+vt+" and "+accDist+" having interior angle "+interiorAngle+" is "+u); double newVelocity = u / time; System.out.println("****New delta position length is, forms basis for new velocity per second "+u); h.put("velocity", newVelocity); //Next calculate the new angleWRTGravity called alpha double alpha = lawOfSinesAngle(u,interiorAngle,accDist); //For some reason accDist is better to use than vt... alpha = 180 - alpha - interiorAngle; System.out.println("Setting new angleWRTGravity (oldAngle|newAngle) "+angleWRTGravity+"/"+alpha); //new angleWRTGravity h.put("angleWRTGravity", alpha);

//**** //**** Step 2: Calculate length t which is the distance to the COG, from this calculate the new length s which is the new distance to COG //**** System.out.println("");

System.out.println("Step 2: Calculate length t which is the distance to the COG, from this calculate the new length s which is the new distance to COG"); double t = distanceFromCOGKilometers; if (accDist > t) { System.out.println("!!!!Gravity pulled object through the COG origin!!!! Collision detection."); //Right now this is how we end h.put("collisionDetection", true); } else { h.put("collisionDetection", false); } double s = lawOfCosines(u,t,alpha); h.put("distanceFromCOG", s); System.out.println("(t(old distanceFromCOG)|s(new distanceFromCOG)) "+t+"/"+s); //**** //**** Step 3: Calculate new angle WRT to axes, new theta //**** System.out.println(""); System.out.println("Step 3: Calculate new angle WRT to axes, new theta"); double newAngleWRTAxes = lawOfSinesAngle(s,alpha,u); System.out.println("(angleWRTAxes|newAngleWRTAxes) "+angleAxesOffset+"/"+newAngleWRTAxes); double totalOffset = (angleAxesOffset + 360; System.out.println("Total offset "+totalOffset); h.put("angleAxesOffset",totalOffset); //need to add the origin //TODO: Checkout what happens here //0.01,1,45,150 //jumps from one side of the origin to the other newAngleWRTAxes) %

//final step, calculate the new x,y position of the object moving the gravity field //we have an angle and a length (polar co-ordindates) so we can covert into //a new x,y position. double rAngle = convertDegreesToRadians(90-totalOffset); System.out.println("X (s|90-totalOffset|Math.cos) "+s+"/"+(90-totalOffset)+"/"+Math.cos(rAngle));

System.out.println("Y (s|90-totalOffset|Math.sin) "+s+"/"+(90-totalOffset)+"/"+Math.sin(rAngle)); double newx = s*Math.cos(rAngle) + x1COG; double newy = s*Math.sin(rAngle) + y1COG; System.out.println("newx "+newx); System.out.println("newy "+newy); h.put("x1", newx); h.put("y1", newy); double deltax = newx - x1Obj; double deltay = newy - y1Obj; System.out.println("deltax "+deltax); System.out.println("deltay "+deltay);

System.out.println("------------------------------------------------------------------------------------------- ITERATION END "); return h; }

/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub PiSpaceTrajectory pse = new PiSpaceTrajectory(PiSpaceTrajectory.UNIT_METRES_PER_HOUR); //calculate next orbital position //example is Earth //x=0,y=150000000 //150000000 kilometers from the Earth //suns gravity on earth = 42.1 kms //travelling at 29.8 km per second //tangent 90 degrees //per second = 1000 miliseconds double SunMass = 1.99E30; double mass1 = 1; //EarthMass; double mass2 = SunMass; double distance = 1.5E11; //EarthRadius in meters; //double gravity = pse.calcGravity(mass1,mass2,distance); double kmFromEarth = 50; //150000000;

System.out.println(""); System.out.println("0 degrees");

pse.calculateNextTrajectoryPosition(0, 0, kmFromEarth, 0, 0, 42.1, 29.8, 0, 1000, 50); System.out.println(""); System.out.println("90 degrees"); HashMap hm = pse.calculateNextTrajectoryPosition(0, 0, kmFromEarth, 0, 0, 42.1, 29.8, 90, 1000, 50); for (int i=0; i<4; i++) { System.out.println(""); hm = pse.calculateNextTrajectoryPosition(hm); } System.out.println(""); System.out.println("180 degrees"); pse.calculateNextTrajectoryPosition(0, 0, kmFromEarth, 0, 0, 42.1, 29.8, 180, 1000, 50); System.out.println(""); System.out.println("270 degrees"); pse.calculateNextTrajectoryPosition(0, 0, kmFromEarth, 0, 0, 42.1, 29.8, 270, 1000, 50); } }

package com.physics.math; import java.util.HashMap; /* * * * * Test program to demonstrate the Pi-Space Theory. * * Author : Martin Brady * * 'ps' methods indicate Pi-Space * * * */ public class PiSpaceExamples { public final double SPEED_OF_LIGHT_KILOMETRES_PER_SECOND = 300000.0; public final double SPEED_OF_LIGHT_METRES_PER_SECOND = 299792458;

public final double SPEED_OF_LIGHT_MILES_PER_SECOND = 186000.0; public static final int UNIT_METRES_PER_HOUR = 1; public static final int UNIT_MILES_PER_HOUR = 2; public int unit; public int getUnit() { return unit; } public double hoursToSeconds(double hours) { //return hours; return hours / 3600.0; } public double secondsToHours(double seconds) { //return seconds; return seconds * 3600.0; } public double cSquared() { if (getUnit() == UNIT_METRES_PER_HOUR) { return SPEED_OF_LIGHT_METRES_PER_SECOND * SPEED_OF_LIGHT_METRES_PER_SECOND; } else { return SPEED_OF_LIGHT_MILES_PER_SECOND * SPEED_OF_LIGHT_MILES_PER_SECOND; } } public double c() { if (getUnit() == UNIT_METRES_PER_HOUR) { return SPEED_OF_LIGHT_METRES_PER_SECOND; } else { return SPEED_OF_LIGHT_MILES_PER_SECOND; } } public double psEnergyToNewton(double energy) { return energy * cSquared(); } public double psVelocityToEnergy(double velocity) { return ((velocity*velocity) / (cSquared())); } /* * * Pi-Space Kinetic Energy * * input per hour e.g. 20 MPH * */ public double psKE(double velocity) { double vSec = hoursToSeconds(velocity); double vProp = vSec / c();

double vVal = 1.0 - Math.cos(Math.asin(vProp)); double backToSeconds = vVal * cSquared(); return secondsToHours(backToSeconds); } /* * * newtonian pe to velocity * */ public double newtPeToVelocity(double pe) { return Math.sqrt(2*pe); } /* * * Planet Gravitational Potential Energy * */ public double gpe(double mass, double radius) { return ( (ugc() * mass) / radius); } /* * * Universal Gravitational Constant * */ public double ugc() { double ret = 6.67300E-11; if (getUnit() == PiSpaceExamples.UNIT_METRES_PER_HOUR) { return 6.67300E-11; } return ret; } /* * * Pi-Space Escape Potential Energy * * Returns a velocity from energy change * * pass in a newtonian energy, return a newtonian velocity * */ public double psPEtoVelocity(double potEnergy) { double areaProp = potEnergy / (c()*c()); double newtVel = Math.sin(Math.acos(1-(areaProp))) * c(); return newtVel; }

/* * * Newtonian Kinetic Energy * */ public double newtonKE(double velocity) { double hs = hoursToSeconds(velocity); double hke = 0.5*(hs*hs); return secondsToHours(hke); } public void setUnit(int unit) { this.unit = unit; }

public PiSpaceExamples(int unit) { setUnit(unit);

} /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub PiSpaceExamples pse = new PiSpaceExamples(PiSpaceExamples.UNIT_METRES_PER_HOUR); double mph = 3600.0; System.out.println("Velocity = "+ mph +" Meters Per Hour"); System.out.println("Newtonian KE ="+pse.newtonKE(mph)); System.out.println("Pi-Space KE ="+pse.psKE(mph)); pse = new PiSpaceExamples(PiSpaceExamples.UNIT_MILES_PER_HOUR); System.out.println("Velocity = "+ mph +" Miles Per Hour"); System.out.println("Newtonian KE ="+pse.newtonKE(mph)); System.out.println("Pi-Space KE ="+pse.psKE(mph));

pse = new PiSpaceExamples(PiSpaceExamples.UNIT_METRES_PER_HOUR); double EarthMass = 5.98E24; double EarthRadius = 6.378E6; double massOfPlanet = EarthMass; double radiusOfPlanet = EarthRadius; double gpe = pse.gpe(massOfPlanet, radiusOfPlanet);

System.out.println("Mass of planet "+massOfPlanet); System.out.println("Radius of planet "+radiusOfPlanet); System.out.println("Gravitation Potential Energy "+gpe); System.out.println("Newton Escape Velocity "+pse.newtPeToVelocity(gpe)); System.out.println("Pi-Space Escape Velocity "+pse.psPEtoVelocity(gpe));

} }

package com.physics.math;

import java.awt.geom.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.print.PrinterJob; import java.awt.event.*; import java.awt.*; import javax.swing.*; import java.awt.print.*; import java.util.HashMap;

public class OrbitChart extends JPanel implements Printable, ActionListener {

final static Color bg = Color.white; final static Color fg = Color.black; final static Color red = Color.red; final static Color white = Color.white;

final static BasicStroke stroke = new BasicStroke(2.0f); final static BasicStroke wideStroke = new BasicStroke(8.0f);

final static float dash1[] = {10.0f}; final static BasicStroke dashed = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f); final static JButton button = new JButton("Print"); final static JButton redo = new JButton("Redo");

final static JFormattedTextField gravFld = new JFormattedTextField(); final static JFormattedTextField radiusFld = new JFormattedTextField(); final static JFormattedTextField velocityFld = new JFormattedTextField(); final static JFormattedTextField angleFld = new JFormattedTextField(); final static JFormattedTextField stepsFld = new JFormattedTextField(); final static JFormattedTextField x1Fld = new JFormattedTextField(); final static JFormattedTextField y1Fld = new JFormattedTextField(); final static JFormattedTextField cogx1Fld = new JFormattedTextField(); final static JFormattedTextField cogy1Fld = new JFormattedTextField(); final static JFormattedTextField timeFld = new JFormattedTextField();

public OrbitChart() { setBackground(bg); button.addActionListener(this); redo.addActionListener(this); gravFld.setColumns(4); radiusFld.setColumns(4); velocityFld.setColumns(4); angleFld.setColumns(4); stepsFld.setColumns(4); x1Fld.setColumns(4); y1Fld.setColumns(4); cogx1Fld.setColumns(4); cogy1Fld.setColumns(4); timeFld.setColumns(4);

public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof JButton) { JButton j = (JButton) e.getSource();

if (j.getText().equals("Print")) { PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setPrintable(this); if (printJob.printDialog()) { try { printJob.print(); } catch (Exception ex) { ex.printStackTrace(); } } }

if (j.getText().equals("Redo")) {

setGrav((Double)gravFld.getValue()); setRadius((Double)radiusFld.getValue()); setVelocity((Double)velocityFld.getValue()); setAngle((Double)angleFld.getValue()); setSteps((Double)stepsFld.getValue()); setX1((Double)x1Fld.getValue()); setY1((Double)y1Fld.getValue()); setCogx1((Double)cogx1Fld.getValue()); setCogy1((Double)cogy1Fld.getValue()); setTime((Double)timeFld.getValue()); this.repaint(); } } }

public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; drawShapes(g2); }

double grav = 10; double velocity = 40; double steps = 185; double angle = 95; double radius = 50; double x1 = 0; double y1 = 50; double cogx1 = 0; double cogy1 = 0; double time = 1000;

public double getTime() { return time; }

public void setTime(double time) { this.time = time; }

public double getRadius() { return radius; }

public void setRadius(double radius) { this.radius = radius; }

public double getX1() { return x1; }

public void setX1(double x1) { this.x1 = x1; }

public double getY1() { return y1; }

public void setY1(double y1) { this.y1 = y1; }

public double getCogx1() { return cogx1; }

public void setCogx1(double cogx1) { this.cogx1 = cogx1; }

public double getCogy1() { return cogy1; }

public void setCogy1(double cogy1) {

this.cogy1 = cogy1; }

public double getGrav() { return grav; }

public void setGrav(double grav) { this.grav = grav; }

public double getVelocity() { return velocity; }

public void setVelocity(double velocity) { this.velocity = velocity; }

public double getSteps() { return steps; }

public void setSteps(double steps) { this.steps = steps; }

public double getAngle() { return angle; }

public void setAngle(double angle) { this.angle = angle; }

public void drawShapes(Graphics2D g2){ Dimension d = getSize();

Color fg3D = Color.lightGray;

g2.setPaint(fg3D); g2.setPaint(fg);

// draw Rectangle2D.Double g2.setStroke(stroke);

g2.setPaint(fg3D);

g2.translate(200, 200);

g2.draw(new Line2D.Double(-150,0,150,0)); g2.draw(new Line2D.Double(0,-150,0,150));

g2.drawString("150", 0, -150); g2.drawString("150", 150, 0);

PiSpaceTrajectory pse = new PiSpaceTrajectory(PiSpaceExamples.UNIT_METRES_PER_HOUR); //double kmFromEarth = 50; //150000000;

double x1; double y1; double x2; double y2;

x1 = getX1(); y1 = -getY1();

HashMap hm = pse.calculateNextTrajectoryPosition(0, getX1(), getY1(), getCogx1(), getCogy1(), grav, velocity, angle, getTime(), getRadius());

x2 = (Double) hm.get("x1"); y2 = - (Double) hm.get("y1");

System.out.println("*******************************************************");

for (int i=0; i<steps; i++) { //System.out.println(""); hm = pse.calculateNextTrajectoryPosition(hm);

boolean collisionDetection = (Boolean) hm.get("collisionDetection");

if (collisionDetection) { System.out.println("Collision detection! End."); break; }

System.out.println("step is "+(i+1)+" mod is "+(i%2));

if (i % 2 == 0) { g2.setPaint(Color.blue); } else { g2.setPaint(Color.red); }

g2.draw(new Line2D.Double(x1,y1,x2,y2));

x1 = x2; y1 = y2;

x2 = (Double) hm.get("x1"); y2 = - (Double) hm.get("y1"); }

public int print(Graphics g, PageFormat pf, int pi) throws PrinterException { if (pi >= 1) { return Printable.NO_SUCH_PAGE; } drawShapes((Graphics2D) g); return Printable.PAGE_EXISTS; }

public static void main(String s[]){ WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} public void windowClosed(WindowEvent e) {System.exit(0);} };

OrbitChart o = new OrbitChart();

JFrame f = new JFrame(); f.addWindowListener(l); JPanel panel = new JPanel();

panel.add(new JLabel("Grav")); gravFld.setValue(o.getGrav()); panel.add(gravFld);

panel.add(new JLabel("Radius")); radiusFld.setValue(o.getRadius()); panel.add(radiusFld);

panel.add(new JLabel("Vel")); velocityFld.setValue(o.getVelocity()); panel.add(velocityFld);

panel.add(new JLabel("Angle")); angleFld.setValue(o.getAngle()); panel.add(angleFld);

panel.add(new JLabel("Steps")); stepsFld.setValue(o.getSteps()); panel.add(stepsFld);

panel.add(new JLabel("X1")); x1Fld.setValue(o.getX1()); panel.add(x1Fld);

panel.add(new JLabel("Y1")); y1Fld.setValue(o.getY1()); panel.add(y1Fld);

panel.add(new JLabel("X1COG")); cogx1Fld.setValue(o.getCogx1()); panel.add(cogx1Fld);

panel.add(new JLabel("Y1COG")); cogy1Fld.setValue(o.getCogy1()); panel.add(cogy1Fld);

panel.add(new JLabel("TimeMS")); timeFld.setValue(o.getTime()); panel.add(timeFld);

panel.add(redo); panel.add(button);

f.getContentPane().add(BorderLayout.SOUTH, panel); f.getContentPane().add(BorderLayout.CENTER, o); f.setSize(1000, 500); f.show(); }

Das könnte Ihnen auch gefallen