Android Open Source - zmap Vector






From Project

Back to project page zmap.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project zmap listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.zmap.geom;
//from w ww. j a v a2 s.  c o  m
public class Vector implements IVector
{
    public Vector()
    {
        vector = new double[3];
    }

    public Vector(double x, double y, double z)
    {
        this();
        setX(x);
        setY(y);
        setZ(z);
    }

    /**
     * @note Instantiate a vector pointed from a to b.
     */
    public Vector(IPoint from, IPoint to)
    {
        this();
        
        if(from == null || to == null)
        {
            throw new NullPointerException();
        }
        if(from.isEmpty() || to.isEmpty())
        {
            throw new EmptyGeometryException();
        }

        setX(to.getX() - from.getX());
        setY(to.getY() - from.getY());
        setZ(to.getZ() - from.getZ());
    }
    
    @Override
    public double getX()
    {
        return vector[X_INDEX];
    }

    @Override
    public double getY()
    {
        return vector[Y_INDEX];
    }

    @Override
    public double getZ()
    {
        return vector[Z_INDEX];
    }

    @Override
    public double[] get()
    {
        return vector;
    }

    @Override
    public void setX(double x)
    {
        vector[X_INDEX] = x;
    }

    @Override
    public void setY(double y)
    {
        vector[Y_INDEX] = y;
    }

    @Override
    public void setZ(double z)
    {
        vector[Z_INDEX] = z;
    }

    @Override
    public void set(double[] v)
    {
        vector[X_INDEX] = v[X_INDEX];
        vector[Y_INDEX] = v[Y_INDEX];
        vector[Z_INDEX] = v[Z_INDEX];
    }

    @Override
    public IVector crossProduct(IVector w)
    {
        if(w == null)
        {
            throw new NullPointerException();
        }

        double vx = getX();
        double vy = getY();
        double vz = getZ();
        double wx = w.getX();
        double wy = w.getY();
        double wz = w.getZ();

        IVector p = new Vector();
        p.setX(vy * wz - vz * wy);
        p.setY(vz * wx - vx * wz);
        p.setZ(vx * wy - vy * wx);
        return p;
    }

    public double dotProduct(IVector w)
    {
        if(w == null)
        {
            throw new NullPointerException();
        }
        
        double d = getX() * w.getX() +
                getY() * w.getY() +
                getZ() * w.getZ();
        return d;
    }

    public double norm()
    {
        return Math.sqrt(getX() * getX() +
                         getY() * getY() +
                         getZ() * getZ());
    }
    
    private double[] vector;

    private static final int X_INDEX = 0;
    private static final int Y_INDEX = 1;
    private static final int Z_INDEX = 2;
}




Java Source Code List

com.zmap.MainActivity.java
com.zmap.geom.EmptyGeometryException.java
com.zmap.geom.Envelope.java
com.zmap.geom.GeometryUtil.java
com.zmap.geom.IEnvelope.java
com.zmap.geom.IGeomCollection.java
com.zmap.geom.IGeomObject.java
com.zmap.geom.ILineOperator.java
com.zmap.geom.IPointCollectionEdit.java
com.zmap.geom.IPointCollection.java
com.zmap.geom.IPoint.java
com.zmap.geom.IPolygon.java
com.zmap.geom.IPolyline.java
com.zmap.geom.IRingCollection.java
com.zmap.geom.IRing.java
com.zmap.geom.IVector.java
com.zmap.geom.InvalidGeometryException.java
com.zmap.geom.PointCollection.java
com.zmap.geom.Point.java
com.zmap.geom.Polyline.java
com.zmap.geom.Ring.java
com.zmap.geom.Vector.java