/* * Copyright (c) 2008-2009 Bill Whitacre http://rampancy.g0dsoft.com * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include "vector.h" void vector2_init(vector2 *vector) { vector->x=0; vector->y=0; } void vector2_set(vector2 *vector,rat_real x,rat_real y) { vector->x=x; vector->y=y; } void vector2_angleunit(vector2 *vector,rat_real angle) { vector->x=cos(angle); vector->y=sin(angle); } rat_real vector2_dot(vector2 a,vector2 b) { return a.x*b.x+a.y*b.y; } rat_real vector2_cross(vector2 a,vector2 b) { return a.x*b.y-a.y*b.x; } vector2 vector2_add(vector2 a,vector2 b) { vector2 n; n.x=a.x+b.x; n.y=a.y+b.y; return n; } vector2 vector2_sub(vector2 a,vector2 b) { vector2 n; n.x=a.x-b.x; n.y=a.y-b.y; return n; } vector2 vector2_multf(vector2 a,rat_real val) { vector2 n; n.x=a.x*val; n.y=a.y*val; return n; } vector2 vector2_multvec(vector2 a,vector2 b) { vector2 n; n.x=a.x*b.x; n.y=a.y*b.y; return n; } void vector2_plus(vector2 *self,vector2 oth) {*self=vector2_add(*self,oth);} void vector2_minus(vector2 *self,vector2 oth) {*self=vector2_sub(*self,oth);} void vector2_timesf(vector2 *self,rat_real val) {*self=vector2_multf(*self,val);} void vector2_timesvec(vector2 *self,vector2 oth) {*self=vector2_multvec(*self,oth);} rat_real vector2_magnitudesquared(vector2 vector) { return vector.x*vector.x+vector.y*vector.y; } vector2 vector2_average(vector2 *pts,unsigned int numpts) { register unsigned int i; vector2 sum={0,0}; if (numpts==0) return sum; if (numpts==1) return *pts; for (i=0; i