Maths - sfvec2f java

related files
/*Title:      mjbWorld
Copyright (c) 1998-2007 Martin John BakerThis program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.For information about the GNU General Public License see http://www.gnu.org/To discuss this program http://sourceforge.net/forum/forum.php?forum_id=122133 also see website https://www.euclideanspace.com/ *//* for theory see: https://www.euclideanspace.com/maths/algebra/vectors/index.htm */package mjbModel; import java.io.*; // for steamtokenizer import java.lang.ref.*; import java.util.*; // for StringTokenizer /* x3d definition<!ENTITY % SFVec2f "CDATA"> <!-- Vector2Float --> *//** * holds a two dimentional vector **/ public class sfvec2f extends property {/** * controls number of digits when saved to a file * VRML only supports float but allow override if higher resolution required */ static boolean saveAsDouble = false ; /** value of vector in x dimention */ public double x;/** value of vector in y dimention */ public double y;/** constructer initialise to zero length */ public sfvec2f() { }/** constructer initialise to value x1 and y1 * @param x1 value of vector in x dimention * @param y1 value of vector in y dimention */ public sfvec2f(float x1,float y1) { x=x1; y=y1; }/** copy constructor * @param p copy values from this instance */ public sfvec2f(sfvec2f p) { x=p.x; y=p.y; }public sfvec2f(double x1,double y1) { x=x1; y=y1; }/** static method to get the VRML name of this property * @return VRML name of this property */ public static String vrmlType_s(){ return "SFVec2f"; }/** <summary> * method to get the VRML name of this property, need a non static method so * that it can be overriden * @return VRML name of this property */ public String vrmlType(){ return "SFVec2f"; }/** override of clone for this class * @return a new instance with the same values as this */ public Object clone() { return new sfvec2f(this); }/** create an array of this type * @param size size of the array * @return array of sfvec2f */ public property[] createArray(int size){ return new sfvec2f[size]; }/** used when reading XML * called by sfparam which is called by mfparam which is called by filter_x3d * * expects val to be in following format (1.0 2.0) * @param val string containing the value * @param type */ public void setAttribute(String val,String type){ try { int v=0; //value being read StringTokenizer st = new StringTokenizer(val,"() \t\n\r\f"); // skip these tokens while (st.hasMoreElements()) { try { switch (v) { case 0: x = Double.parseDouble(st.nextToken());break; case 1: y = Double.parseDouble(st.nextToken());break; default: st.nextToken();break; // skip token } v++; // valid number was read so goto next } catch (NumberFormatException e) { // if it is not a valid number continue while loop } } } catch (Exception e) { System.out.println("sfvec2f.setAttribute("+val+","+type+") " + e.toString()); } } public sfvec2f minus() { return new sfvec2f(-x,-y); }/** for theory see: https://www.euclideanspace.com/maths/algebra/vectors/index.htm */ public void sub(sfvec2f other){ x-= other.x; y-= other.y; }/** for theory see: https://www.euclideanspace.com/maths/algebra/vectors/index.htm */ public void add(sfvec2f other){ x+= other.x; y+= other.y; }/** returns true if v2 has same value of this * @param v2 vector to compare with * @return true if v2 has same value of this */ boolean equals(sfvec2f v2) { if (v2==null) return false; if (x!= v2.x) return false; if (y!= v2.y) return false; return true; }/** output as a string // mode values // 0 - output modified values // 1 - output original values // 2 - output attribute // 3 - output attribute in brackets // 4 - output with f prefix */ public String outstring(int format) { if (format == 3) { if (saveAsDouble) return "(" + x + " " + y + ")"; else return "(" + new Float(x).toString() + " " + new Float(y).toString() + ")"; } else if (format == 4) { return new Float(x).toString() + "f," + new Float(y).toString() + "f"; } else { if (saveAsDouble) return "" + x + " " + y; else return new Float(x).toString() + " " + new Float(y).toString(); } }/** write to file // filter = information about output // mode values // 0 - output VRML97 modified values // 1 - output VRML97 original values // 2 - output xml (x3d) // 3 - output attribute in brackets // 4 - output with f prefix */ public void write(filter f,int mode,int indent){ f.write(outstring(mode)); } /** * returns string representing this vector in a form that can be used in source code * @return string representing this vector in a form that can be used in source code */ public String toStatic() { return new Double(x).toString() + "f," + new Double(y).toString() + "f"; }/** input a value to this vector from a file * used by mfparam.vrml2par * @param f * @param sfp * @param n * @param mode */ public boolean instring(filter f,sfparam sfp,nodeBean n,int mode) { String s; try { s=f.nextToken(); if (s!=null) if (s.equals("IS")) { s=f.nextToken(); if (sfp!=null) sfp.setIs(s); return true; } x = Float.parseFloat(s); s=f.nextToken(); y = Float.parseFloat(s); } catch (Exception e) { System.out.println("sfvec2f.instring error: " + e.toString()); } return true; }/** parse string to set value of this vector * @param f * @param s1 string containing this vector */ public boolean instring(filter f,String s1) { String s; try { x = Float.parseFloat(s1); s=f.nextToken(); y = Float.parseFloat(s); } catch (Exception e){ System.out.println("sfvec2f.instring error: " + e.toString()); } return true; }/** * returns type of class which can edit this * @return type of class which can edit this * */ static public Class getEditClass(){ return sfvec2fEditor.class; } }

metadata block
see also:

 

Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

cover Mathematics for 3D game Programming - Includes introduction to Vectors, Matrices, Transforms and Trigonometry. (But no euler angles or quaternions). Also includes ray tracing and some linear & rotational physics also collision detection (but not collision response).

Terminology and Notation

Specific to this page here:

 

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2023 Martin John Baker - All rights reserved - privacy policy.