|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic |
Last Thread Next Thread ![]() |
| Implementing 2D Vectors |
|
![]() Anonymous Poster |
||||
|
||||
| Hi, Just a quick note about a bug I found in this class. If you pass in 360 degrees for the angle, you get a bug. If SetAngle/SetLength get 360 degrees, they should convert that to 0. The way I did it was the following. void C2DVector::SetLength( long Lin ) { assert(TablesInitialised); length = Lin; int circle_angle = angle%360; x = (long) floor(costable[circle_angle]*length + 0.5); y = (long) floor(sintable[circle_angle]*length + 0.5); } void C2DVector::SetAngle( int Ain ) { assert(TablesInitialised); angle = Ain % 360; x = (long) (costable[angle]*length); y = (long) (sintable[angle]*length); } This way even if the thing turning goes way past 360, the code still works. Hope this is helpful. Cheers DC dave at geko dot net dot au |
||||
|
||||
![]() iMalc Member since: 3/5/2004 |
||||
|
|
||||
| Bug: The copy-constructor needs to take a const-reference. Bug: HitBoundary may not handle negative angles correctly. InitTables could use cosf & sinf instead of sin & cos. C++ code should ideally be using C++ style casts. Coming from a 3D background, I personally would never store the angle & length AND x & y data in a vector. If I were using photoshop-like swirly image transforms where I needed to store stuff in polar coordinates I'd store angle and length. Otherwise I'd only store x & y (Vectors implicitly start at {0, 0}). I'd use dot-products or perp-Products and other such things for reflections and the like. By all means, I'd perhaps have a constructor that takes angle and length. I guess it all depends on what you want to do with it. Heck, you could even make it store the data like a variant and work either way, with functions to convert between the different internal representations. In the past when I've used a lookup table for sine & cos, I've overlapped them such that it's only a quarter more for the cos table after the sine table. One could go furthur than this, but going that far has no speed decrease whatsoever. However, I've been told that nowdays it's probably equally as fast to simply call sin(), when you take into account both (a) cache misses cost from the table and (b) the loss of accuracy using a table. He probably didn't do this to keep it simple for a tutorial, but I'd still advise against using a table at all. That article is old/outdated, and buggy/inefficient. I would not recommend using it. I much like this templated vector class for use with C++: VectorSpace If you're wondering if you think that article should be ditched or revamped, I'd say: Yep. |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|