2 * Copyright 2006 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.wamblee.gps.geometry;
19 import java.io.Serializable;
22 * Coordinates in some 3-dimensional coordinate system.
24 * @author Erik Brakkee
26 public class Coordinates implements Serializable {
33 * Constructs the coordinates.
34 * @param aX1 First coordinate.
35 * @param aX2 Second coordinate.
36 * @param aX3 Third coordinate.
38 public Coordinates(double aX1, double aX2, double aX3) {
44 public double getX1() {
48 public double getX2() {
52 public double getX3() {
56 public double getX(int i) {
62 throw new IllegalArgumentException("coordinate out of range " + i);
66 * @see java.lang.Object#toString()
69 public String toString() {
70 return "(" + getX1() + ", " + getX2() + ", " + getX3() + ")";
73 public Coordinates add(Coordinates aC) {
74 return new Coordinates(_x1 + aC._x1, _x2 + aC._x2, _x3 + aC._x3);
77 public Coordinates subtract(Coordinates aC) {
78 return new Coordinates(_x1 - aC._x1, _x2 - aC._x2, _x3 - aC._x3);
81 public double innerProduct(Coordinates aC) {
82 return _x1 * aC._x1 + _x2 * aC._x2 + _x3 * aC._x3;
85 public Coordinates outerProduct(Coordinates aC) {
86 return new Coordinates(
87 _x2*aC._x3 - _x3*aC._x2,
88 -_x1*aC._x3 + _x3*aC._x1,
89 _x1*aC._x2 - _x2*aC._x1
93 public double norm() {
94 return Math.sqrt(innerProduct(this));
97 public Coordinates scale(double aMultiplier) {
98 return new Coordinates(_x1*aMultiplier, _x2*aMultiplier, _x3*aMultiplier);
101 public Coordinates normalize() {
102 return scale(1.0/norm());