• Advertisement
Sign in to follow this  

the & question

This topic is 4729 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[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??

Share this post


Link to post
Share on other sites
Advertisement
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();
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement