Skip to main content
gamedev.net
🔒 Locked

the & question

Started by derek7 Mar 15, 2005 at 4:26 AM 1 replies 400+ views
Original Post
derek7
derek7
[source ="cpp"]#include <iostream>
#include <d3dx9.h>
using namespace std;
D3DXVECTOR3 closestPointAtPlane(D3DXVECTOR3 *  point,D3DXVECTOR3 * n,float d)
{
	float distance=D3DXVec3Dot(point ,n)-d;
	return *point-distance*(*n);
}
main()
{
	D3DXVECTOR3 point(3,6,9);
	D3DXVECTOR3 n(.4838,.8062,-.1613);
	float d=-42;
	D3DXVECTOR3 P=closestPointAtPlane(&point,&n,d);
	cout<<P<<endl;

}
1.If I using closestPointAtPlane(D3DXVECTOR3 *& point,D3DXVECTOR3 * & n,float d) it can't be compiled? why?? 2. how to I use cout to output P??
snk_kid
snk_kid
Quote:
Original post by derek7
1.If I using closestPointAtPlane(D3DXVECTOR3 *& point,D3DXVECTOR3 * & n,float d) it can't be compiled? why??


whats with the pointer for?

typedef D3DXVECTOR3 vector3f;vector3f closestPointAtPlane(const vector3f& point,                             const vector3f& n,                             float d) {   float distance = D3DXVec3Dot(&point, &n) - d;   return point - distance * n;}


I just had to make a type alias of D3DXVECTOR3 its just plain ugly to look at [smile]

Quote:
Original post by derek7
2. how to I use cout to output P??


Lots of ways, you could overload the stream extraction operator like so:

#include <sstream>typedef D3DXVECTOR3 vector3f;template< typename CharT, typename Traits >std::basic_ostream<CharT, Traits>&operator<<(std::basic_ostream<CharT, Traits>& os, const vector3f& x) {   std::basic_ostringstream<CharT, Traits> s;   s.flags(os.flags());   s.imbue(os.getloc());   s.precision(os.precision());   s << '(' << x.x << ',' << x.y << ',' << x.z ')';   return os << s.str();}
ApochPiQ
ApochPiQ
You don't have to specify the "D3DXVECTOR3 *" part before the & operator, because you aren't casting the pointer - the closestPointAtPlane() function expects two D3DXVECTOR3 * pointers, and both point and n are of type D3DXVECTOR3. Taking the address of a D3DXVECTOR3 variable (the & operator) will return a pointer of type D3DXVECTOR3 *. So the call can just look like this:

P = closestPointAtPlane(&point, &n, d);


snk_kid handled number 2 quite effectively I think, so I'll leave that one as is.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.