Maths - sfvec2f C#

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
   */namespace mjbModel { 
   using System;
   using System.ComponentModel;
   using System.IO;
   using System.Collections;
   using System.Reflection;
   using System.Text;
   using System.Xml;/// <summary>
   /// holds a two dimentional vector
   /// </summary>
   class sfvec2f : property {/// <summary>
   /// controls number of digits when saved to a file
   /// VRML only supports float but allow override if higher resolution required
   /// </summary>
   static bool saveAsDouble = false ;
   /// <summary>
   /// value of vector in x dimention
   /// </summary>
   public double x;
   /// <summary>
   /// value of vector in y dimention
   /// </summary>
   public double y;/// <summary>
   /// constructer initialise to zero length
   /// </summary>
   public sfvec2f() {
   }/// <summary>
   /// constructer initialise to value x1 and y1
   /// </summary>
   /// <param name="x1">value of vector in x dimention</param>
   /// <param name="y1">value of vector in y dimention</param>
   public sfvec2f(float x1,float y1) {
   x=x1;
   y=y1;
   }/// <summary>
   /// copy constructor
   /// </summary>
   /// <param name="p">copy values from this instance</param>
   public sfvec2f(sfvec2f p) {
   x=p.x;
   y=p.y;
   }/// <summary>
   /// static method to get the VRML name of this property
   /// </summary>
   /// <returns>VRML name of this property</returns>
   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
   /// </summary>
   /// <returns>VRML name of this property</returns>
   public override string vrmlType(){
   return "SFVec2f";
   }/// <summary>
   /// returns type of class which can edit this
   /// </summary>
   /// <returns>type of class which can edit this</returns>
   public static Type getEditClass(){
   return typeof(sfvec2fEditor);
   }/// <summary>
   /// override of clone for this class
   /// </summary>
   /// <returns>a new instance with the same values as this</returns>
   public override property clone() {
   return new sfvec2f(this);
   }/// <summary>
   /// create an array of this type
   /// </summary>
   /// <param name="size">size of the array</param>
   /// <returns>array of sfvec2f</returns>
   public override property[] createArray(int size){
   return new sfvec2f[size];
   }/// <summary>
   /// returns true if v2 has same value of this
   /// </summary>
   /// <param name="v2">vector to compare with</param>
   /// <returns>true if v2 has same value of this</returns>
   bool Equals(sfvec2f v2) {
   if (v2==null) return false;
   if (x!= v2.x) return false;
   if (y!= v2.y) return false;
   return true;
   }/// <summary>
   /// 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)
   /// </summary>
   /// <param name="val">string containing the value</param>
   /// <param name="type"></param>
   public override void setAttribute(string val,string type){
   try {
   string[] tokens=val.Split("() \t\n\r\f".ToCharArray());
   int offset=0;
   while (tokens[offset].Equals("")) offset++;
   x = Double.Parse(tokens[offset]);
   y = Double.Parse(tokens[offset+1]);
   } catch (Exception e){
   Console.WriteLine("sfvec2f.setAttribute("+val+","+type+")    " + e);
   }
   }/// <summary>
   /// output as a string
   /// </summary>
   /// <param name="format">mode values
   /// 0 - output modified values
   /// 1 - output original values
   /// 2 - output attribute
   /// 3 - output attribute in brackets
   /// 4 - output with f prefix</param>
   /// <returns>string representing this vector</returns>
   public override String outstring(int format) {
   if (format == 3) {
   if (saveAsDouble)
   return String.Concat("(",x.ToString(),
   " ",y.ToString(),")");
   else
   return String.Concat("(",((float)x).ToString(),
   " ",((float)y).ToString(),")");
   } else if (format == 4) {
   return String.Concat(x.ToString(),"f,",
   y.ToString(),"f");
   } else {
   if (saveAsDouble)
   return String.Concat(x.ToString(),
   " ",y.ToString());
   else
   return String.Concat(((float)x).ToString(),
   " ",((float)y).ToString());
   }
   }/// <summary>
   /// write to file
   /// </summary>
   /// <param name="f">filter = information about output</param>
   /// <param name="mode">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</param>
   /// <param name="indent"></param>
   public override void write(filter f,int mode,int indent){
   f.write(outstring(mode));
   }/// <summary>
   /// returns string representing this vector in a form that can be used in source    code
   /// </summary>
   /// <returns>string representing this vector in a form that can be used    in source code</returns>
   public String toStatic() {
   return String.Concat(x.ToString(),"f,",
   y.ToString(),"f");
   }/// <summary>
   /// input a value to this vector from a file
   /// used by mfparam.vrml2par
   /// </summary>
   /// <param name="f"></param>
   /// <param name="sfp"></param>
   /// <param name="n"></param>
   /// <param name="mode"></param>
   /// <returns></returns>
   public override bool 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 = Single.Parse(s);
   s=f.nextToken();
   y = Single.Parse(s);
   } catch (Exception e) {
   Console.WriteLine("sfvec2f.instring {0}",e);
   }
   return true;
   }/// <summary>
   /// parse string to set value of this vector
   /// </summary>
   /// <param name="f"></param>
   /// <param name="s1">string containing this vector</param>
   /// <returns></returns>
   public bool instring(filter f,String s1) {
   String s;
   try {
   x = Single.Parse(s1);
   s=f.nextToken();
   y = Single.Parse(s);
   } catch (Exception e){
   Console.WriteLine("sfvec2f.instring {0}",e);
   }
   return true;
   } } // class
   } //namespace mjbModel
 

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.