java 矩形重叠问题,矩形重叠问题,碰撞算法,Objective-C实现

1: //

2: // Rectangle.m

3: // Rectangle_prac_S4

4: //

5: // Created by Jeff on 04.01.11.

6: // Copyright 2011 __MyCompanyName__. All rights reserved.

7: //

8:

9: #import "Rectangle.h"

10: @implementation Rectangle

11: @synthesize width,height;

12: -(void)setWidth:(double)w andHeight:(double)h

13: {

14: width=w;

15: height=h;

16: }

17: -(void)setOrigin:(XYPoint *)pt

18: {

19: if(origin)

20: {

21: [origin release];

22: }

23: origin=[[XYPoint alloc] init];

24: [origin setX:pt.x andY:pt.y];

25: }

26: -(XYPoint *)origin

27: {

28: return origin;

29: }

30:

31: -(Rectangle *)intersect:(Rectangle *)vRect

32: {

33: Rectangle *result=[[Rectangle alloc] init];

34: XYPoint *resultPoint=[[XYPoint alloc] init];

35:

36: //得到两个矩形的中心点坐标

37:

38: XYPoint *midPoint=[[XYPoint alloc] init];

39: XYPoint *vMidPoint=[[XYPoint alloc] init];

40:

41: double mx,my,mvx,mvy;

42: mx=(origin.x+width)/2;

43: my=(origin.y+height)/2;

44: mvx=(vRect.origin.x+vRect.width)/2;

45: mvy=(vRect.origin.y+vRect.height)/2;

46: [midPoint setX:mx andY:my];

47: [vMidPoint setX:mvx andY:mvy];

48:

49:

50: //如果在x方向上 中心点间距离差的绝对值小于等于两个矩形宽的和的1/2,

51: //而且在y方向上中心点间距离差的绝对值小于等于两个矩形高的和的1/2,矩形重叠

52:

53: if (abs(((midPoint.x+width/2)-(vMidPoint.x+vRect.width/2)))<=((width+vRect.width)/2) &&

54: abs(midPoint.y+height/2-vMidPoint.y-vRect.height/2)<=((height+vRect.height)/2))

55: {

56: //矩形重叠

57: //得到重叠矩形的原点坐标

58: double maxX,maxY,cX,cY;

59: //二者相比较,始终取坐标大的那个

60: maxX=origin.x>=vRect.origin.x?origin.x:vRect.origin.x;

61: maxY=origin.y>=vRect.origin.y?origin.y:vRect.origin.y;

62: //取原点坐标对角线上的另一个坐标(cX,cY)

63: //始终取坐标小的那个

64: cX=(vRect.origin.x+vRect.width)>=(origin.x+width)?(origin.x+width):(vRect.origin.x+vRect.width);

65: cY=(vRect.origin.y+vRect.height)>=(origin.y+height)?(origin.y+height):(vRect.origin.y+vRect.height);

66: //确定原点坐标,计算出长度和宽度

67: [resultPoint setX:maxX andY:maxY];

68: result.origin=resultPoint;

69: result.width=abs(maxX-cX);

70: result.height=abs(maxY-cY);

71: //释放两个矩形中间点坐标内存

72: [midPoint release];

73: [vMidPoint release];

74: return result;

75:

76: }else {

77: [resultPoint setX:0 andY:0];

78: result.origin=resultPoint;

79: result.width=0;

80: result.height=0;

81: [midPoint release];

82: [vMidPoint release];

83: return result;

84: }

85:

86: }

87:

88: -(double)area

89: {

90: return (double)height*width;

91: }

92: -(double)perimeter

93: {

94: return ((double)height+width)*2;

95: }

96:

97: -(XYPoint *)translate:(XYPoint *)ptv

98: {

99:

100: XYPoint *result=[[XYPoint alloc] init];

101: [result setX:origin.x+ptv.x andY:origin.y+ptv.y];

102:

103: return result;

104:

105: }

106:

107: -(void)dealloc

108: {

109: if (origin) {

110: [origin release];

111: }

112: [super dealloc];

113: }

114:

115: -(void)draw

116: {

117: for(int i=0;i

118: printf("-");

119: printf("\n");

120: for(int i=0;i

121: {

122: printf("|");

123: for(int j=0;j

124: printf("");

125: printf("|");

126: printf("\n");

127: }

128: for(int i=0;i

129: printf("-");

130: printf("\n");

131: }

132: @end

133:

测试intersect方法:

1: #import "Rectangle.h"

2: #import "XYPoint.h"

3:

4: int main (int argc, const char * argv[]) {

5: NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

6: Rectangle *myRect=[[Rectangle alloc] init];

7: XYPoint *myPoint=[[XYPoint alloc] init];

8:

9: [myPoint setX:200 andY:100];

10: [myRect setWidth:300 andHeight:200];

11: myRect.origin=myPoint;

12:

13: NSLog(@"rectangle w=%g,h=%g",myRect.width,myRect.height);

14:

15: NSLog(@"origin at (%g,%g)",myRect.origin.x,myRect.origin.y);

16:

17: //重叠测试

18: Rectangle *testRect=[[Rectangle alloc] init],*newRect;

19: XYPoint *testPoint=[[XYPoint alloc] init];

20: [testPoint setX:300 andY:50];

21: testRect.origin=testPoint;

22: testRect.height=350;

23: testRect.width=100;

24: NSLog(@"testRect w=%g,h=%g",testRect.width,testRect.height);

25:

26: NSLog(@"origin at (%g,%g)",testRect.origin.x,testRect.origin.y);

27: newRect= [myRect intersect:testRect];

28: NSLog(@"重叠矩形原点坐标为(%g,%g) width is %g height is %g",newRect.origin.x,newRect.origin.y,newRect.width,newRect.height);

29:

30:

31: [testPoint release];

32: [testRect release];

33: [newRect release];

34: [myRect release];

35: [myPoint release];

36:

37:

38: [pool drain];

39: return 0;

40: }

41:

测试结果:

2011-01-07 22:51:31.139 Rectangle_prac_S4[915:80f] rectangle w=300,h=200

2011-01-07 22:51:31.182 Rectangle_prac_S4[915:80f] origin at (200,100)

2011-01-07 22:51:31.207 Rectangle_prac_S4[915:80f] testRect  w=100,h=350

2011-01-07 22:51:31.218 Rectangle_prac_S4[915:80f] origin at (300,50)

2011-01-07 22:51:31.222 Rectangle_prac_S4[915:80f] 重叠矩形原点坐标为(300,100)  width is 100 height is 200

试试别的参数,应该也是正确的。.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值