001 package org.wdssii.core; 002 003 /** 004 * Subtracting two CPoints gives rise to a CVector Based on Thomas Vaughan's 005 * CVector class in the C++ version. 006 * 007 * @author Lakshman 008 * @version $Id: CVector.java,v 1.1 2006/12/27 20:48:09 lakshman Exp $ 009 */ 010 public class CVector { 011 public double x; 012 013 public double y; 014 015 public double z; 016 017 /** 018 * everything in kilometers. the z direction is the polar axis. x is from 019 * center through the Greenwich meridian y is from the center of earth 020 * through the Caribbean. 021 */ 022 public CVector(double x, double y, double z) { 023 this.x = x; 024 this.y = y; 025 this.z = z; 026 } 027 028 public CVector unit() { 029 double len = norm(); 030 if (len <= 0) 031 return this; // zero vector 032 return multiply(1.0 / len); 033 } 034 035 public double norm() { 036 return Math.sqrt(normSquared()); 037 } 038 039 public double normSquared() { 040 return (x * x + y * y + z * z); 041 } 042 043 public CVector multiply(double f) { 044 return new CVector(x * f, y * f, z * f); 045 } 046 047 public CVector plus(CVector v) { 048 return new CVector(x + v.x, y + v.y, z + v.z); 049 } 050 051 public CVector minus(CVector v) { 052 return new CVector(x - v.x, y - v.y, z - v.z); 053 } 054 055 public double dotProduct(CVector v) { 056 return x * v.x + y * v.y + z * v.z; 057 } 058 059 public CVector crossProduct(CVector v) { 060 return new CVector(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y 061 * v.x); 062 } 063 064 public String toString() { 065 return "(vector: " + x + "," + y + "," + z + ")"; 066 } 067 }