-2
//Test code to print all coordinates

CGRect b=CGRectMake(0, 0, 4, 3);
//top left
float topLX=CGRectGetMinX(b);
float topLY=CGRectGetMinY(b);

NSLog(@"(%f,%f)",topLX,topLY);

//top right
float topRX=CGRectGetMaxX(b);
float topRY=CGRectGetMinY(b);

NSLog(@"(%f,%f)",topRX,topRY);

//bottom left
float bottomLX=CGRectGetMinX(b);
float bottomLY=CGRectGetMaxY(b);

NSLog(@"(%f,%f)",bottomLX,bottomLY);

//bottom right
float bottomRX=CGRectGetMaxX(b);
float bottomRY=CGRectGetMaxY(b);

NSLog(@"(%f,%f)",bottomRX,bottomRY);


//Sample CGRectContainsPoint Test

CGRect d=CGRectMake(0, 0, 4, 3);
CGPoint p=CGPointMake(0, 0);
CGPoint o=CGPointMake(4, 3);

BOOL contains=CGRectContainsPoint(d, p);
BOOL contains1=CGRectContainsPoint(d, o);

if(contains) NSLog(@"yes"); else NSLog(@"no");
//This will print yes because p is inside rect b


if(contains1) NSLog(@"yes");else NSLog(@"no");
//This will print no because o is inside rect b

NSLog Output:
2014-06-16 16:08:37.291 Pirate Adventure[7564:60b] (0.000000,0.000000)
2014-06-16 16:08:37.291 Pirate Adventure[7564:60b] (4.000000,0.000000)
2014-06-16 16:08:37.292 Pirate Adventure[7564:60b] (0.000000,3.000000)
2014-06-16 16:08:37.292 Pirate Adventure[7564:60b] (4.000000,3.000000)
2014-06-16 16:08:37.292 Pirate Adventure[7564:60b] yes
2014-06-16 16:08:37.293 Pirate Adventure[7564:60b] no

私は CGRect と一連のタイル オブジェクトの描画に取り組んできました。しかし、どのように描いても、横 4 ポイント (幅) と高さ 3 ポイント (高さ) をマッピングする正方形を取得できません。また、ここでも一日中試行錯誤しながら徹底的に探しました。

4

2 に答える 2

2

CGRectContainsPointのドキュメントから。

討論

点の座標が長方形の内側にあるか、最小 Xエッジまたは最小 Y エッジにある場合、点は長方形の内側にあると見なされます。

最大エッジ ポイントに対してチェックしているようです。そのため、2 回目の呼び出しで が返さfalseれます-CGRectContainsPoint

于 2014-06-16T23:34:05.870 に答える
0

一日中作業した後、これがゼロベースの CGRect であることがわかりました。つまり、0 ~ 3 が 4 ポイントに等しいということです。また、0-2で勝ち点3。したがって、ポイントは原点として 0,0 をカウントするため、CGRect の 4 つのポイントは次のようになります: 0,0 - 3,0 2,0 - 2,3

于 2014-06-17T14:35:03.993 に答える