矢量函数 [英] vector functions

查看:93
本文介绍了矢量函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

如果有人可以向我解释下面的代码会很棒。


//两个向量之间的返回角度

const float inline Angle(const CVector& normal)const

{

返回acosf(* this%normal);

}


//用正常向量反射此向量表面

const CVector内联反射(const CVector& normal)const

{

const CVector vec(* this | 1); //规范化这个载体

return(vec - normal * 2.0 *(vec%normal))*!* this;

}


//围绕法线旋转角度

const CVector内联旋转(常量浮动角度,常量CVector&正常)const

{

const float cosine = cosf(angle);

const float sine = sinf(angle);


返回CVector(* this * cosine +((正常) * * this)*(1.0f - 余弦))*

正常+(*这^正常)*正弦);

}


这产生了以下错误:


vector.h:220:错误:声明没有声明任何内容

vector.h:220:错误:`inline'之前的语法错误'

vector.h:226:错误:ISO C ++禁止在返回类型中定义类型

vector.h:226:错误:语法`inline'之前的错误''

vector.h:233:错误:`inline'之前的语法错误''

vector.h:236:错误:`angle''是未在此范围内声明

vector.h:238:错误:返回'之前'语法错误


%和^产生什么?为什么在

函数定义的末尾还有一个const?

感谢和问候

Michael

Hello
If someone could explain the code below to me would be great.

// return angle between two vectors
const float inline Angle(const CVector& normal) const
{
return acosf(*this % normal);
}

// reflect this vector off surface with normal vector
const CVector inline Reflection(const CVector& normal) const
{
const CVector vec(*this | 1); // normalize this vector
return (vec - normal * 2.0 * (vec % normal)) * !*this;
}

// rotate angle degrees about a normal
const CVector inline Rotate(const float angle, const CVector& normal) const
{
const float cosine = cosf(angle);
const float sine = sinf(angle);

return CVector(*this * cosine + ((normal * *this) * (1.0f - cosine)) *
normal + (*this ^ normal) * sine);
}

This is producing the following errors:

vector.h:220: error: declaration does not declare anything
vector.h:220: error: syntax error before `inline''
vector.h:226: error: ISO C++ forbids defining types within return type
vector.h:226: error: syntax error before `inline''
vector.h:233: error: syntax error before `inline''
vector.h:236: error: `angle'' was not declared in this scope
vector.h:238: error: syntax error before `return''

What do % and ^ produce? Why is there a const also at the end of the
function definition?
THANKS and regards
Michael

推荐答案

" Michael Sgier" < SG *** @ nospam.ch> schrieb im Newsbeitrag

news:43 ********************** @ news.sunrise.ch ...
"Michael Sgier" <sg***@nospam.ch> schrieb im Newsbeitrag
news:43**********************@news.sunrise.ch...
你好
如果有人能解释下面的代码对我来说会很棒。

//两个向量之间的返回角度
const float inline Angle(const CVector& normal) const
返回acosf(* this%normal);

//用法线向量反射此向量表面
const CVector inline Reflection (const CVector& normal)const
{const CVector vec(* this | 1); //规范化这个向量
return(vec - normal * 2.0 *(vec%normal))*!* this;
}
//旋转角度约为法线
const CVector inline Rotate(常量浮点角,常量CVector& normal)
const {
const float cosine = cosf(angle);
const float sine = sinf(angle); <返回CVector(* this * cosine +((normal * * this)*(1.0f - cosine))*
normal +(* this ^ normal)* sine);
}

这产生了以下错误:

vector.h:220:错误:声明没有声明任何内容
vector.h:220:错误:在内联之前的语法错误'
vector.h:226:错误:ISO C ++禁止在返回类型中定义类型
vector.h:226:错误:内联之前的语法错误
vector.h:233:错误:`inline'之前的语法错误'
vector.h:236:错误:`angle''未在此范围内声明
vector.h:238:错误:语法'return''之前的错误


粘贴整个班级。或者更好:尽可能减少你的代码和

然后在这里粘贴你的课程。

%和^会产生什么?


%是模数运算符。它返回除法的余数。

^是按位异或运算符。它比较每一位。如果其中一个

为0而另一个为1,则结果中的相应位也为1,

否则为0.

为什么在
函数定义的末尾还有一个const?


如果将

函数声明为const,则不允许类的函数修改成员变量。

感谢和问候
Michael
Hello
If someone could explain the code below to me would be great.

// return angle between two vectors
const float inline Angle(const CVector& normal) const
{
return acosf(*this % normal);
}

// reflect this vector off surface with normal vector
const CVector inline Reflection(const CVector& normal) const
{
const CVector vec(*this | 1); // normalize this vector
return (vec - normal * 2.0 * (vec % normal)) * !*this;
}

// rotate angle degrees about a normal
const CVector inline Rotate(const float angle, const CVector& normal) const {
const float cosine = cosf(angle);
const float sine = sinf(angle);

return CVector(*this * cosine + ((normal * *this) * (1.0f - cosine)) *
normal + (*this ^ normal) * sine);
}

This is producing the following errors:

vector.h:220: error: declaration does not declare anything
vector.h:220: error: syntax error before `inline''
vector.h:226: error: ISO C++ forbids defining types within return type
vector.h:226: error: syntax error before `inline''
vector.h:233: error: syntax error before `inline''
vector.h:236: error: `angle'' was not declared in this scope
vector.h:238: error: syntax error before `return''
Paste your whole class. Or better: Reduce your code as much as possible and
then paste your class here.
What do % and ^ produce?
% is the modulus operator. It returns the remainder of a division.
^ is the bitwise-exclusive-or operator. It compares each bit. If one of them
is 0 and the other one is 1, the corresponding bit in the result is also 1,
otherwise it is 0.
Why is there a const also at the end of the
function definition?
Functions of a class are not allowed to modify the member variables if a
function was declared as const.
THANKS and regards
Michael




问候Chris



Greetings Chris


/ *

const float inline Angle(const CVector& normal)const

{

返回acosf(* this%normal);

}

* /


%以上应该是标量乘积,以便计算* this

与正常之间的角度,这通常是使用%来交叉

产品是不好的,它经常被用在像这样的代码中。将它用于

点/标量产品更奇怪。


只要对这种奇怪的评论,如果我知道

哪条线是哪条(例如哪条错误对应哪条线)。我打赌

某些地方的某些声明错过了(也许标题

不是自包含的,或者需要特定的包含顺序,

无论如何,打败我没有看到更多的代码.. :)

/*
const float inline Angle(const CVector& normal) const
{
return acosf(*this % normal);
}
*/

Above the % should be scalar product so that the angle between *this
and normal would be computed, it''s generally a bad to use % for cross
product, which it is often seen used in code like this. Using it for
dot/scalar product is even more strange.

Just commenting on that oddity, I could comment on the errors if I knew
which line was which (eg. which error correspond to which line). I bet
somewhere along the line some declaration was missed (maybe the headers
are not self-contained or require specific order of inclusion,
whatever, beats me w/o seeing more of the code.. :)


我只是无法再拒绝回复..我一直盯着代码

难以置信:真的奇怪地使用了运算符重载。 +, - ,/,* ..

仍然很酷且很有用,因为其含义很容易理解,

但......这里发生的事情是无法相信的。令人难以置信的东西..没有

进攻! 8P

I just cannot resisting replying again.. I keep staring at the code in
disbelief: really bizarre use of operator overloading. +, -, /, * ..
are are still cool and useful since the meaning is easily understood,
but... what is happening here is beyond belief.. incredible stuff.. no
offence! 8P


这篇关于矢量函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆