使用指针或函数声明数组 [英] Declaring Arrays using Pointers or functions

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

问题描述

#include <iostream>
using namespace std;
int main()
{
    Vectorf point(3);
    point.set(0,0,3.0);
    point.set(1,0,2.0);
    point.set(2,0,1.0);
    
    double angle = 0.0;
    float sx = 0.5, sy = 0.5;
    float ax = 2.0, ay = 2.0;
    float tx = 0.0, ty = 1.0;
    double const Pi = 4 * atan(1.0);
    angle = Pi / 6 ;
    cout<< "The result of Rotation (angle).Skew(s).Scaling(a).
Translation(t) X M of point is(M'') " << endl << 
        (Rotate_R(Skew_s_X(Skew_s_Y(Scale_a(Translate_t(point,tx,ty),ax,ay),sx),sy),angle));
    cout << endl;
    return 0;
}


我必须将矩阵M={{''3''},{''2''},{''1''}};乘以
(Rotate_R(Skew_s_X(Skew_s_Y(Scale_a(Translate_t(point,tx,ty),ax,ay),sx),sy),angle));
如何声明矩阵并将其乘以上述函数... ???

解决方案

我强烈怀疑您的代码会编译.例如,Vectorf是什么(新鲜的地狱)?声明在哪里?
而且,在不知道Rotate_R,Skew_s_X,...函数原型的情况下,我们怎么能猜出答案呢?只是因为我想我知道您要做什么,并且熟悉3D线性空间中的变换数学:

1.我假设所有这些函数Rotate_R(),Skew_s_X(),Scale_a()等都是线性变换
2.这些函数中的每个函数都将一个点作为参数,对其进行转换,然后返回转换后的点.
3.这些点是2D点,因为您似乎只使用x和y值来描述您的变换.因此,变换是2空间的变换.
4. Vectorf是代表点的类型. Vectorf的系数很可能是浮点数,因为您也在转换中使用了浮点数变量. (后缀"f"也是赠品)
5. M是一个值矩阵,它本身描述了一个尚未定义的线性变换.
*我说的是未定义的,因为您给出的定义是3x1矩阵,或者是向量,而不是2x2矩阵,因为您需要能够将其乘"到2空间中的任何东西上.我想您会误读M的外观,或者在这里将其误解了.
6.出于某种原因,您具有描述转换的函数(请参见1),但是您描述的任务涉及在函数的帮助下将矩阵乘以所描述的嵌套转换.这使我假设您自己定义了这些功能,而与实际任务无关.

解决方案:
大多数变换(请参见1)是2空间中的线性变换,因此可以描述为2x2矩阵.在点P(2x1向量)上应用变换M(2x2矩阵)时,从数学上来说,您要做的是计算乘积Q = M * P.

在P上应用多个转换时,您当然可以将其连接起来并计算R = M2 * Q = M2 *(M * P).通过将每个转换实现为将一个点转换为另一个点的函数,并通过嵌套函数调用来串联转换,您做了同样的事情.

现在,当您不仅要对一个点,还要对许多点P1,P2,P3 ...应用变换时,您要做什么?您可以为课程的每个点进行嵌套调用.但是有一个更好的方法:考虑上面的公式-R = M2 *(M * P).在这个公式中,乘法是关联的,您也可以这样写:R =(M2 * M)* P.现在定义MTotal = M2 * M,您将得到R = MTotal * P.对于在多个点上应用级联转换,您可以再次使用MTotal并将其在每个点上相乘,而不是逐步应用每个单独的转换.

这是您必须执行的操作:不要将各种转换定义为以点作为参数并计算出结果作为点的函数,而是将它们定义为矩阵.为旋转定义矩阵R,为歪斜定义矩阵SX和SY,为缩放变换定义矩阵S.定义矩阵乘法(如果您不知道怎么做,可以轻松地用谷歌搜索),然后通过乘以矩阵来计算组合的变换.

不幸的是,平移(我指的是函数Translate_t)不是线性变换,因此不能表示为2x2矩阵.有多种方法可以解决此问题,但是我已经没时间了,无论如何,根据您任务的措辞,可以不必在最终转换矩阵中包含此转换.

我希望我的假设不会完全消失,而这堵文字墙实际上可以帮助您完成某些工作,或者至少可以帮助其他人在我离开的地方继续学习-我现在要去度假.祝你好运!


#include <iostream>
using namespace std;
int main()
{
    Vectorf point(3);
    point.set(0,0,3.0);
    point.set(1,0,2.0);
    point.set(2,0,1.0);
    
    double angle = 0.0;
    float sx = 0.5, sy = 0.5;
    float ax = 2.0, ay = 2.0;
    float tx = 0.0, ty = 1.0;
    double const Pi = 4 * atan(1.0);
    angle = Pi / 6 ;
    cout<< "The result of Rotation (angle).Skew(s).Scaling(a).
Translation(t) X M of point is(M'') " << endl << 
        (Rotate_R(Skew_s_X(Skew_s_Y(Scale_a(Translate_t(point,tx,ty),ax,ay),sx),sy),angle));
    cout << endl;
    return 0;
}


i have to multiply a matrix M={{''3''},{''2''},{''1''}}; to
(Rotate_R(Skew_s_X(Skew_s_Y(Scale_a(Translate_t(point,tx,ty),ax,ay),sx),sy),angle));
how to declare a matrix and multiply it to the above thing...???

解决方案

I strongly doubt your code would compile. For instance what (the fresh hell) is Vectorf? Where is it declared?
Moreover, without knowing Rotate_R,Skew_s_X,... function prototypes, how can we guess the answer?


I am going to make somea helluvalot assumptions, but only because I think I know what you are trying to do and am familiar with the mathematics of transformations in 3D linear space:

1. I assume all these functions Rotate_R(), Skew_s_X(), Scale_a(), etc. are linear transformations
2. Each of these functions take a point as an argument, apply a transformation on it, and return the transformed point.
3. The points are 2D points, since you only seem to be using x and y values to describe your transformations. The transformations are therefore transformations of 2-space.
4. Vectorf is a type that represents a point. The coefficients of your Vectorf are most likely floats, as you are using float variables for your transformations as well. (the ''f'' suffix is also a giveaway)
5. M is a matrix of values that itself describes an as of yet undefined* linear transformation.
*I say undefined, because the definition you gave is a 3x1 matrix, or a vector rather than a 2x2 matrix as you would need to be able to ''multiply'' it onto anything from 2-space. I presume you misread what M should look like, or misrepresented it here.
6. For some reason you have functions to describe transformations (see1), but the task you describe involves multiplying a matrix by the nested transformation described with the help of your functions. This causes me to assume that you defined these functions yourself, and without regard to the actual task.

Solution:
Most of the transformations (see 1) are linear transformations in 2-space, and therefore can be described as a 2x2 matrix. When you apply a transformation M (a 2x2 matrix) on a point P (a 2x1 vector), what you do, mathematically, is compute the product Q=M*P.

When you apply more than one transformation on P, you can of course concatenate this and calculate R=M2*Q=M2*(M*P). You did the same thing, by implementing each transformation as a function that transforms a point into another point and you concatenated the transformation by nesting the function calls.

Now, what do you do, when you want to apply a transformation on not just one, but on many points P1, P2, P3,... ? You could do the nested calls for each point of course. But there is a better way: Consider the formula above - R=M2*(M*P). In this formula, the multiplication is associative, you can also write it like this: R=(M2*M)*P. Now define MTotal=M2*M and you get R=MTotal*P. And for applying the concatenated transformations on many points, you can again just use MTotal and multiply it on each point, rather than step by step applying each individual transformation.

This is what you must do here: instead of defining your various transformations as functions that take a point as an argument and calculate a point as a result, define them as a matrix. Define a Matrix R for your rotation, a matrix SX and SY for your skewings, and a matrix S for your scaling transformation. Define matrix multiplication (easy enough to google for if you don''t know how), and calculate your combined transformation by multiplying the matrices.

Unfortunately, a translation (I''m referring to your function Translate_t) is not a linear transformation and therefore cannot be represented as a 2x2 matrix. There are ways to resolve this, but I''m running out of time, and anyway, depending on the wording of your task it may not be neccesary to include this transformation in your final transformation matrix.

I hope my assumptions weren''t completely off and this wall of text actually helps you accomplish something, or at least helps others to pick up where I left - I am off for a holiday now. Good luck!


这篇关于使用指针或函数声明数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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