-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolygonShape.m
153 lines (119 loc) · 4.7 KB
/
PolygonShape.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// PolygonShape.m
// WhatATool2
//
// Created by Ed on 28/06/09.
// Copyright 2009 Ed Lui. All rights reserved.
//
#import "PolygonShape.h"
@implementation PolygonShape
@synthesize numberOfSides;
@synthesize minimumNumberOfSides;
@synthesize maximumNumberOfSides;
#pragma mark Initialization methods
- (void)dealloc {
// NSLog(@"dealloc is being called");
[super dealloc];
}
- (id)init {
return [self initWithNumberOfSides:kMinimumNumberOfSides minimumNumberOfSides:kMinimumNumberOfSides maximumNumberOfSides:kMaximumNumberOfSides];
}
- (id)initWithNumberOfSides:(int)sides {
return [self initWithNumberOfSides:sides minimumNumberOfSides:kMinimumNumberOfSides maximumNumberOfSides:kMaximumNumberOfSides];
}
- (id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min {
return [self initWithNumberOfSides:sides minimumNumberOfSides:min maximumNumberOfSides:kMaximumNumberOfSides];
}
- (id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max {
if (self = [super init]) {
minimumNumberOfSides = kMinimumNumberOfSides;
maximumNumberOfSides = kMaximumNumberOfSides;
numberOfSides = minimumNumberOfSides;
[self setNumberOfSides:sides];
[self setMinimumNumberOfSides:min];
[self setMaximumNumberOfSides:max];
}
return self;
}
#pragma mark Setter methods
// use a validation to ensure sides is within min and max
- (void)setNumberOfSides:(int)sides {
if (sides != numberOfSides && [self isValidNumberOfSides:sides]) {
numberOfSides = sides;
}
}
// use a validation to ensure minimum sides is greater than 2, less than maximum
- (void)setMinimumNumberOfSides:(int)sides {
if (sides != minimumNumberOfSides && [self isValidMinimumNumberOfSides:sides]) {
minimumNumberOfSides = sides;
}
}
// use a validation to ensure maximium is less than or equal to 12, greater than minimum
- (void)setMaximumNumberOfSides:(int)sides {
if (sides != maximumNumberOfSides && [self isValidMaximumNumberOfSides:sides]) {
maximumNumberOfSides = sides;
}
}
#pragma mark Validation methods
// validation method to ensure sides are within min and max
- (BOOL)isValidNumberOfSides:(int)sides {
if (sides < minimumNumberOfSides) {
// NSLog(@"Invalid number of sides: %d is less than the minimum of %d allowed", sides, minimumNumberOfSides);
return NO;
}
if (sides > maximumNumberOfSides) {
// NSLog(@"Invalid number of sides: %d is greater than the maximum of %d allowed", sides, maximumNumberOfSides);
return NO;
}
return YES;
}
// validation method to ensure side is greater than 2, and smaller than max
- (BOOL)isValidMinimumNumberOfSides:(int)sides {
if (sides < 3) {
// NSLog(@"Invalid number of sides: %d is less than the minimum of %d allowed", sides, minimumNumberOfSides);
return NO;
}
if (sides >= [self maximumNumberOfSides]) {
// NSLog(@"Invalid number of sides: %d is greater than the maximum of %d allowed", sides, maximumNumberOfSides);
return NO;
}
return YES;
}
// validation method to ensure side is less than or equal to 12, and greater than min
- (BOOL)isValidMaximumNumberOfSides:(int)sides {
if (sides > 12) {
// NSLog(@"Invalid number of sides: %d is greater than the maximum of %d allowed", sides, minimumNumberOfSides);
return NO;
}
if (sides <= [self minimumNumberOfSides]) {
return NO;
}
return YES;
}
#pragma mark Angle methods
// angle in degrees is (180 * (numberOfSides - 2)) / numberOfSides
- (float)angleInDegrees {
// return (float) ((180 * ([self numberOfSides] - 2)) / (float)[self numberOfSides]);
return (180.0 * ([self numberOfSides] - 2)) / [self numberOfSides];
}
// angle in radians is (angle in degrees * pi) / 180
- (float)angleInRadians {
// return (float) ([self angleInDegrees] * ( M_PI / (float) 180));
return [self angleInDegrees] * (M_PI / 180.0);
}
#pragma mark Name and description methods
// array of shape names, with numberOfSides as index
- (NSString *)name {
NSArray *shapeNames = [[NSArray alloc] initWithObjects:
@"Impossible", @"Monogan", @"Digon", @"Triangle", @"Rectangle", @"Pentagon", @"Hexagon",
@"Heptagon", @"Octagon", @"Nonagon", @"Decagon", @"Hendagon", @"Dodecagon", nil];
[shapeNames autorelease];
return [shapeNames objectAtIndex:(numberOfSides)];
}
- (NSString *)description {
NSString *description = [NSString
stringWithFormat:@"Hello, I am a %d-sided polygon (aka a %@) with angles of %d degrees (%.6f radians).",
numberOfSides, [self name], (int) [self angleInDegrees], [self angleInRadians]];
return description;
}
@end