Fart Annoyed Question( about scaling vectors)

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
jamesmfox
Posts: 15
Joined: September 1st, 2017, 10:46 pm
Location: Mississippi, USA

Fart Annoyed Question( about scaling vectors)

Post by jamesmfox » September 5th, 2017, 4:03 pm

In the beginners tutorial 20b, when chili is setting up the grid of bricks, he says to setup the grid rectangle using:

Code: Select all

 RectF( topleft + Vec2( x * brickWidth, y * brickHeight ) );
My Question is would it be wrong to make brickWidth and brickHeight a vector to scale x, y?
like this:

Code: Select all

 RectF( topleft + Vec2( x, y ) * Vec2( brickWidth, brickHeight ) );
Should i use vectors like that? Is it bad form? or does it affect performance?

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Fart Annoyed Question( about scaling vectors)

Post by chili » September 6th, 2017, 12:12 am

In general, vector multiplication means the dot product (x1 * x2 + y1 * y2). There is also the cross product that is sometimes used. What you are doing there is called the Hadamard product, and it is seldom used in linear algebra, so I would not define my operator* to mean that.

In 3D Fundamentals I have a function GetHadamard though, because it is useful for lighting / color calculations.
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Fart Annoyed Question( about scaling vectors)

Post by albinopapa » September 6th, 2017, 4:49 am

Took me awhile to see it, but to multiply two vectors in the manner you want, you would actually want to turn the second vector into a matrix with the X component in the top left of the matrix and the Y component in the bottom right of the matrix. In this way you end up doing the dot product on each column or row of the matrix and the result of each ends up being the result for the X and Y.

Code: Select all

Vec2 a( 1, 3 );
Vec2 b( 4, 5 );
Mat2x2 c
{
     b.x,    0.f,
      0.f,   b.y
};

Vec2 result;
result.x = ( a.x * c.values[0][0] ) + ( a.y * c.values[1][0] );
result.y = ( a.x * c.values[0][1] ) + ( a.y * c.values[1][1] );

// Would be the same as
Vec2 result = Vec2( 
                              DotProduct( a, { c.values[0][0], c.values[1][0] },
                              DotProduct( a, { c.values[0][1], c.values[1][1] }
                    );

// Or, if you have one of the chili frameworks that has his _Vec2 template class
Vec2 result = Vec2( 
                              a * { c[0][0], c[1][0] }, 
                              a * { c[0][1], c[1][1] } 
                    );
// Since he overloads his operator*(const _Vec2 &) function to be the dot product operation.
In this example, the Mat2x2 would be a class of floats in a 2D array

Code: Select all

class Mat2x2
{
public:
     Mat2x2( float V00, float V01, float V10, float V11 );

     float values[2][2];
};
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Fart Annoyed Question( about scaling vectors)

Post by chili » September 6th, 2017, 9:26 am

As part of a larger affine transformation matrix multiplication would be a decent choice, but for this application it's a loser. Added complexity AND lower performance.

In hlsl for example v1 * v2 is per component product (it's used a lot for color / lighting bullshits), where as dot( v1,v2 ) and mul( v1,v2 ) give the dot product.
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Fart Annoyed Question( about scaling vectors)

Post by albinopapa » September 6th, 2017, 2:02 pm

I agree it's a bad fit for this purpose, was simply making an observation or stating a realization.

Did not realize mul(vector, vector) returned dot product, guess I"m glad I never used it for multiplying vectors when I need per component multiplication. I guess, I haven't checked this page out before.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Fart Annoyed Question( about scaling vectors)

Post by chili » September 6th, 2017, 2:36 pm

This is the page where i got that info (in doing a little bit of fact checking before making my post).

https://msdn.microsoft.com/en-us/librar ... s.85).aspx
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Fart Annoyed Question( about scaling vectors)

Post by albinopapa » September 7th, 2017, 3:04 am

Holy crap, had to read the entire page to find it so blatant about what mul does lol. Thanks for the link chili.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply