長方形で異なる計算を行う複数の関数を持つCプログラムがあります。長方形と長方形の点をマッピングするために構造体を定義しました。長方形のポインターと2つのint値をパラメーターとして受け取り、それらのパラメーターに基づいて長方形を移動する関数(moveR)で立ち往生しています。
[![ここに画像の説明を入力してください] [1]] [1]私が遭遇している問題は、関数が新しい位置を更新しないことです。たとえば、「moveR()」の呼び出しでは、長方形の左上隅のx値を18に移動する必要がありますが、15を返し続けます。助けていただければ幸いです。
#include <stdio.h>
struct point{
int x, y;
} point1, point2, answer, point1b, point2b;
struct rectangle{
struct point upperLeft, lowerRight;
} R1;
struct point centerR (struct rectangle R1){
int centerx= (R1.lowerRight.x-R1.upperLeft.x)/2;
int centery= (R1.lowerRight.y-R1.upperLeft.y)/2;
struct point pointR;
pointR.x= centerx;
pointR.y= centery;
return pointR;
}
struct rectangle moveR(struct rectangle *R1, int x, int y){
R1->upperLeft.x+x;
R1->lowerRight.x+x;
R1->upperLeft.y+y;
R1->lowerRight.y+y;
return *R1;
}
struct rectangle *R2;
int main()
{
point1.x= 6;
point1.y= 6;
point2.x= 10;
point2.y= 12;
R1.upperLeft= point1;
R1.lowerRight= point2;
answer = centerR(R1);
printf("The center of R1 is:\n");
printf("The x value of the center is %d \n", answer.x);
printf("The y value of the center is %d \n", answer.y);
R2= &R1;
point1b.x=15;
point1b.y=15;
point2b.x=25;
point2b.y=25;
R2->upperLeft = point1b;
R2->lowerRight= point2b;
moveR(R2,3,4);
printf("The position of R2 is:\n");
printf("The x value of the upper left is %d \n", R1.upperLeft.x);
return 0;
}```
[1]: https://i.stack.imgur.com/I2pO5.png
あなたはここで数学をします:
R1->upperLeft.x+x;
R1->lowerRight.x+x;
R1->upperLeft.y+y;
R1->lowerRight.y+y;
では、結果をどこに保存しますか?次のように、更新された結果をR1に保存するつもりですか?
R1->upperLeft.x += x;
R1->lowerRight.x += x;
R1->upperLeft.y += y;
R1->lowerRight.y += y;
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加