我正在尝试构建一个好的三角形类,因为我经常使用它们。我决定修改我的类,以便用户可以设置 a bool
,以选择使用度数而不是弧度。有趣的是,除了描述之外,每件事似乎仍然可以正常工作:
IMTriangle *tri = [[IMTriangle alloc]init];
tri.shouldUseDegrees = YES;
tri.SideA = 3;
tri.SideB = 4;
tri.SideC = 5;
[tri solve];
NSLog(@"tri %@", tri);
double A = tri.angleA;
double B = tri.angleB;
double C = tri.angleC;
NSLog(@"A = %f B = %f C = %f",A,B,C);
NSLog(@"tri %@",tri);
给我这个
tri a=3.0000 b=4.0000 c=5.0000 A=0.6435 B=0.9273 C=1.5708 perimeter=6.0000 area=12.0000 height=4.0000 shouldUseDegrees 1
A = 36.869897 B = 53.130098 C = 90.000004
tri a=3.0000 b=4.0000 c=5.0000 A=36.8699 B=53.1301 C=90.0000 perimeter=6.0000 area=12.0000 height=4.0000 shouldUseDegrees 1
在我从三角形对象中获取角度以使用它们之后,它的描述突然如我所料。我不知道它是否与我正在使用的自定义设置器和获取器有关:
-(void) setAngleA:(double)angleA
{
if (!_shouldUseDegrees){
_angleA = angleA;
}
else _angleA = angleA * M_PI/180;
}
-(double) angleA
{
if (!_shouldUseDegrees) {
return _angleA;
}
return _angleA *= 180/M_PI;
}
或者我的描述方法
-(NSString *) description;
{
return [NSString stringWithFormat:@"a=%.4f b=%.4f c=%.4f A=%.4f B=%.4f C=%.4f perimeter=%.4f area=%.4f height=%.4f shouldUseDegrees %i", _sideA , _sideB , _sideC , _angleA , _angleB , _angleC , self.area , self.perimeter , self.height ,_shouldUseDegrees];
}