# Projecting a polygon onto 2D plane

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

## Recommended Posts

Hello

I know that similar questions has been answered earlier but you can't seem to get it right.

if have a plane at (0,0,-1) with a normal (0,0,1) and try to project two points
(-0.1f, +0.1f, -1.0f ), (+0.1f, -0.1f, -1.0f ) I would expect them to come out the same or maybe am I missing something? anyway I get (which can't be correct):

-0.14,0.7,-1
+0.14,0.7,-1

Here is my code for testing...

struct VECTOR{	float x, y, z;};VECTOR VecSub(VECTOR a, VECTOR b){	VECTOR v;	v.x = a.x - b.x;	v.y = a.y - b.y;	v.z = a.z - b.z;	return v;}VECTOR VecCross(VECTOR a, VECTOR b){	VECTOR v;		v.x=a.y*b.z-a.z*b.y;	v.y=a.z*b.x-a.x*b.z;	v.z=a.x*b.y-a.y*b.x;	return v;}VECTOR VecNormalize(VECTOR a){	float l = sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);	a.x /= l;	a.y /= l;	a.z /= l;	return a;}int main(){	VECTOR p1 = { -0.1f, +0.1f, -1.0f };	VECTOR p2 = { +0.1f, -0.1f, -1.0f };	VECTOR n = { 0, 0, 1 };	VECTOR u, v;		VECTOR p1d, p2d;	u = VecSub(p2, p1);		v = VecCross(u, n);	u = VecNormalize(u);	v = VecNormalize(v);	printf("%0.5f %0.5f %0.5f\n", u.x, u.y, u.z);	printf("%0.5f %0.5f %0.5f\n", v.x, v.y, v.z);	printf("%0.5f %0.5f %0.5f\n", n.x, n.y, n.z);	p1d.x = p1.x * u.x + p1.y * u.y + p1.z * u.z;	p1d.y = p1.x * v.x + p1.y * v.y + p1.z * v.y;	p1d.z = p1.x * n.x + p1.y * n.y + p1.z * n.z;	p2d.x = p2.x * u.x + p2.y * u.y + p2.z * u.z;	p2d.y = p2.x * v.x + p2.y * v.y + p2.z * v.y;	p2d.z = p2.x * n.x + p2.y * n.y + p2.z * n.z;	printf("\n");	printf("%0.5f, %0.5f %0.5f\n", p1d.x, p1d.y, p1d.z);	printf("%0.5f, %0.5f %0.5f\n", p2d.x, p2d.y, p2d.z);	return 0;}

##### Share on other sites
Corrections in underlined bold:

p1d.x = p1.x * u.x + p1.y * u.y + p1.z * u.z;
p1d.y = p1.x * v.x + p1.y * v.y + p1.z * v.z;
p1d.z = p1.x * n.x + p1.y * n.y + p1.z * n.z;

p2d.x = p2.x * u.x + p2.y * u.y + p2.z * u.z;
p2d.y = p2.x * v.x + p2.y * v.y + p2.z * v.z;
p2d.z = p2.x * n.x + p2.y * n.y + p2.z * n.z;

You should also have a dot product function to make this operation easier in the future:
VECTOR VecDot(VECTOR a, VECTOR b){	VECTOR v;		v.x=a.x*b.x;	v.y=a.y*b.y;	v.z=a.z*b.z;	return v;}
However at the end of the day, you might be better off using an establised math library instead of writing your own, since mistakes like this can happen often and are difficult to track down. Plus unless you're writing a vector class for learning purposes, this wheel has been invented hundreds of times before :)

Regarding the transformed coordinates, they shouldn't turn out the same, since you're rotating the frame about the Z-axis by 45°. However the y-value should be 0 for both transformed coordinates, which it is with this correction.

##### Share on other sites
Thanks for the reply and the correction.

Since you told me that the values i thought was strange is correct I start to think that I don't what I thought i was doing=)

I thought it shouldn't rotate at all in this case? since its already looking down the Z-axis?

I want to be rotate a plane facing in any direction and have it facing towards positive Z.
How can I accomplish that?

Thanks

##### Share on other sites

Where planeNormal is an unit length vector assigned by x,y,z values 0,0,1 making it aligned with world axis Z.

##### Share on other sites
Quote:
 Original post by ZipsterYou should also have a dot product function to make this operation easier in the future:VECTOR VecDot(VECTOR a, VECTOR b){ VECTOR v; v.x=a.x*b.x; v.y=a.y*b.y; v.z=a.z*b.z; return v;}

Sorry, doesn't dot product return a scalar? as in A.B = A1*B1 + A2*B2 + An*Bn ? Just wondering if there's something here I'm not aware of.

##### Share on other sites
Quote:
 Sorry, doesn't dot product return a scalar? as in A.B = A1*B1 + A2*B2 + An*Bn ?
Yes, that's right. (The code you're asking about might be called a member-wise product, or perhaps a modulation. I've never seen the term 'dot product' used to refer to a member-wise product though.)

##### Share on other sites
Quote:
Original post by jyk
Quote:
 Sorry, doesn't dot product return a scalar? as in A.B = A1*B1 + A2*B2 + An*Bn ?
Yes, that's right. (The code you're asking about might be called a member-wise product, or perhaps a modulation. I've never seen the term 'dot product' used to refer to a member-wise product though.)

Sorry, I wrote a member-wise product for some unknown reason while I was thinking dot product... oh well, just return v.x+v.y+v.z [wink]

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633696
• Total Posts
3013396
×