Skip to content

Instantly share code, notes, and snippets.

@leegrey
Created June 11, 2011 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leegrey/1020522 to your computer and use it in GitHub Desktop.
Save leegrey/1020522 to your computer and use it in GitHub Desktop.
LGVector2D - A 2D Vector Class in C++
/*
* LGVector2D.cpp
*
* Created by Lee Grey on 11/06/11.
* Copyright 2011 Lee Grey. All rights reserved.
*
*/
#include "LGVector2D.h"
#include <iostream>
#include <math.h>
LGVector2D::LGVector2D() {
x = 0;
y = 0;
}
LGVector2D::LGVector2D( float xInit, float yInit ) {
x = xInit;
y = yInit;
}
void LGVector2D::set( LGVector2D& v ) {
x = v.x;
y = v.y;
}
void LGVector2D::set( float xs, float ys ) {
x = xs;
y = ys;
}
LGVector2D LGVector2D::clone() {
return LGVector2D( x, y );
}
void LGVector2D::zero() {
x = 0;
y = 0;
}
void LGVector2D::add( LGVector2D& v ) {
x += v.x;
y += v.y;
}
void LGVector2D::subtract( LGVector2D& v ) {
x -= v.x;
y -= v.y;
}
void LGVector2D::multiply( float s ) {
x *= s;
y *= s;
}
void LGVector2D::divide( float d ) {
x /= d;
y /= d;
}
void LGVector2D::normalize() {
float length = sqrt(x*x+y*y);
if( length == 0 ) y = 1;
x /= length;
y /= length;
}
float LGVector2D::getLength() {
return sqrt(x*x + y*y);
}
void LGVector2D::setLength( float l ) {
float length = sqrt( x*x+y*y );
if( length == 0 ) y = 1;
x = (x / length) * l;
y = (y / length) * l;
}
float LGVector2D::getAngle() {
return atan2( y, x );
}
float LGVector2D::getAngleDeg() {
return atan2(y,x) * 57.2957;
}
void LGVector2D::setAngle( float a ) {
float length = sqrt(x*x+y*y);
x = cos(a)*length;
y = sin(a)*length;
}
void LGVector2D::setAngleDeg( float a ) {
float length = sqrt(x*x+y*y);
a *= 0.0174532925;
x = cos(a)*length;
y = sin(a)*length;
}
void LGVector2D::rotateBy( float l ) {
float angle = getAngle();
float length = sqrt(x*x+y*y);
x = cos(l+angle)*length;
y = sin(l+angle)*length;
}
float LGVector2D::dot( LGVector2D& v ) {
return x * v.x + y * v.y;
}
float LGVector2D::cross( LGVector2D& v ) {
return (x * v.y) - (y * v.x);
}
float LGVector2D::distanceTo( LGVector2D v ) {
LGVector2D distanceVector = LGVector2D(v.x - x, v.y - y );
return distanceVector.getLength();
}
LGVector2D LGVector2D::vectorTo( LGVector2D v ) {
return LGVector2D(v.x - x, v.y - y );
}
void LGVector2D::lookAt( LGVector2D& v ) {
LGVector2D *vectorToTarget = new LGVector2D( v.x - x, v.y - y );
setAngle( vectorToTarget->getAngle() );
}
void LGVector2D::reflect( LGVector2D v ) {
LGVector2D unitNormal = v.unit();
*this = *this - ( unitNormal * ( (unitNormal.dot( *this ) ) * 2 ) );
}
LGVector2D LGVector2D::unit() {
LGVector2D unit = this->clone();
unit.normalize();
return unit;
}
void LGVector2D::invert() {
x = -x;
y = -y;
}
//OPERATORS
LGVector2D LGVector2D::operator+ ( const LGVector2D& v ) const {
return LGVector2D( x + v.x, y + v.y );
}
LGVector2D LGVector2D::operator- ( const LGVector2D& v ) const {
return LGVector2D( x - v.x, y - v.y );
}
LGVector2D LGVector2D::operator* ( const float s ) const {
return LGVector2D( x * s, y * s );
}
//dot product
float LGVector2D::operator* ( const LGVector2D& v ) const {
return (x * v.x + y * v.y);
}
//cross product
float LGVector2D::operator^ ( const LGVector2D& v ) const {
return (x * v.y) - (y * v.x);
}
LGVector2D LGVector2D::operator/ ( const float s ) const {
return LGVector2D( x / s, y / s );
}
LGVector2D LGVector2D::operator/ ( const LGVector2D& v ) const {
return LGVector2D( x / v.x, y / v.y );
}
bool LGVector2D::operator== ( const LGVector2D& v ) const {
return ( x == v.x && y == v.y );
}
void LGVector2D::operator= ( const LGVector2D& v ) {
x = v.x;
y = v.y;
}
void LGVector2D::operator+= ( const LGVector2D& v ) {
x += v.x;
y += v.y;
}
void LGVector2D::operator-= ( const LGVector2D& v ) {
x -= v.x;
y -= v.y;
}
void LGVector2D::operator*= ( const float s ) {
x *= s;
y *= s;
}
void LGVector2D::operator/= ( const float s ) {
x /= s;
y /= s;
}
float LGVector2D::operator[] ( int i ) {
if ( i == 0 ) return x;
else return y;
}
void LGVector2D::print( bool includeEndline ) {
if( includeEndline ) printf( "%s%f%s%f%s", "[" , x , ",", y , "]\n" );
else printf( "%s%f%s%f%s", "[" , x , ",", y , "]" );
}
//convenience///////////////
LGVector2D lgv(float x, float y) {
return LGVector2D(x,y);
}
/*
* Vector2D.h
*
* Created by Lee Grey on 11/06/11.
* Copyright 2011 Lee Grey. All rights reserved.
*
*/
#ifndef _LGVECTOR2D_
#define _LGVECTOR2D_
class LGVector2D {
public :
float x, y;
LGVector2D();
LGVector2D( float xInit, float yInit );
void zero();
void set( LGVector2D& v );
void set( float xs, float ys );
LGVector2D clone();
void setAngle( float a );
void setAngleDeg( float a );
float getAngle();
float getAngleDeg();
void rotateBy( float l );
void add( LGVector2D& v );
void subtract( LGVector2D& v );
void multiply( float s );
void divide( float s );
void normalize();
void setLength( float l );
float getLength();
float dot( LGVector2D& v );
float cross( LGVector2D& v );
float distanceTo( LGVector2D v );
LGVector2D vectorTo( LGVector2D v );
void lookAt( LGVector2D& v );
void reflect( LGVector2D v );
LGVector2D unit();
void invert();
void print( bool includeEndline = true );
LGVector2D operator+ ( const LGVector2D& v ) const;
LGVector2D operator- ( const LGVector2D& v ) const;
LGVector2D operator* ( const float s ) const;
float operator* ( const LGVector2D& v ) const; //dot product
float operator^ ( const LGVector2D& v ) const; //cross product
LGVector2D operator/ ( const float s ) const;
LGVector2D operator/ ( const LGVector2D& v ) const;
bool operator== ( const LGVector2D& v ) const;
void operator= ( const LGVector2D& v );
void operator+= ( const LGVector2D& v );
void operator-= ( const LGVector2D& v );
void operator*= ( const float s );
void operator/= ( const float s );
float operator[] ( int i );
};
LGVector2D lgv(float x, float y);
#endif
@carstenpoulsen
Copy link

Nice job, but many errors.

define _USE_MATH_DEFINES // for C++

is missing for VS purposes. Replace magic constants with M_PI/180.0 etc.
static_cast are not necessary
rotateBy should use:
x' = x * cos(Ɵ) - y * sin(Ɵ)
y' = x * sin(Ɵ) + y * cos(Ɵ)
lookAt uses new, who deletes?
normalize fails for zero length vectors
setLength fails for zero length vectors but should be based on unit and multiply.
What is clone ment for, there is an assignment?
What is the lgv for?

@carstenpoulsen
Copy link

What is the function of 'reflect'?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment