Amazon.com Widgets 8.5 - cool use of Heron's Formula for Triangle class
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 20, 2013, 01:31:25 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 8
| | | |-+  8.5 - cool use of Heron's Formula for Triangle class
Pages: [1]   Go Down
Print
Author Topic: 8.5 - cool use of Heron's Formula for Triangle class  (Read 2385 times)
sir
Full Member
***
Posts: 118


Email




« on: March 15, 2009, 08:04:17 PM »

main.m
Code: (Objective-C)
#import "Rectangle.h"
#import "Circle.h"
#import "Triangle.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Rectangle *myRectangle = [Rectangle new];
Circle *myCircle = [Circle new];
Triangle *myTriangle = [Triangle new];

NSLog(@"Rectange INFO");
myRectangle.fillColor = 4839;
myRectangle.lineColor = 3848;
myRectangle.filled = YES;
myRectangle.print;

NSLog(@"Circle INFO");
myCircle.fillColor = 3333;
myCircle.lineColor - 4444;
myCircle.filled = NO;
myCircle.print;
myCircle.radius = 3;
NSLog(@"The area of the circle is: %f", myCircle.area);
NSLog(@"The circumference of the circle is: %f", myCircle.circumference);

NSLog(@"Triangle INFO");
[myTriangle setSides: 5:4:3];
NSLog(@"The perimeter of the triangle is: %f", myTriangle.perimeter);
NSLog(@"The height of the triangle is: %f", myTriangle.height);
NSLog(@"The area of the triangle is: %f", myTriangle.area);

[myRectangle release];
[myCircle release];
[myTriangle release];
    [pool drain];
    return 0;
}

GraphicObject.h
Code: (Objective-C)
#import <Foundation/Foundation.h>


@interface GraphicObject : NSObject {
int fillColor; //32-bit color
BOOL filled; //Is the object filled?
int lineColor; //32-bit line color
}

@property int fillColor, lineColor;
@property BOOL filled;

-(void) print;

@end

GraphicObject.m
Code: (Objective-C)
#import "GraphicObject.h"


@implementation GraphicObject

@synthesize fillColor, lineColor, filled;

-(void) print {
NSLog(@"The fill color is: %i", fillColor);
NSLog(@"The line color is: %i", lineColor);

if (filled)
NSLog(@"The graphic object is filled.");
else
NSLog(@"The graphic object is not filled.");
}

@end

rectangle.h
Code: (Objective-C)
#import "GraphicObject.h"

@interface Rectangle : GraphicObject {
double width, height;
}

@property double width, height;

-(void) setWidth: (double) w andHeight: (double) h;
-(double) area;
-(double) perimeter;
@end

rectangle.m
Code: (Objective-C)
#import "Rectangle.h"

@implementation Rectangle

@synthesize width, height;


-(void) setWidth: (double) w andHeight: (double) h {
width = w;
height = h;
}

-(double) area {
return width * height;
}

-(double) perimeter {
return (width +height) * 2;
}

@end

Triangle.h
Code: (Objective-C)
/*
This triangle class utilizes Heron's Formula to determine
the height of the triangle with all sides known!

h = (2/a)(SQRT(x(x-a)(x-b)(x-c)))
x = (a+b+c)/2 or
a,b,c = sides
a is base (90deg from height)

Thus, the inputs to this class are sides only; no height.
*/

#import "GraphicObject.h"

@interface Triangle : GraphicObject {
double s1, s2, s3, x, h; //s1 is the base, x is half the sum of the bases
}

@property double s1, s2, s3;

-(void) setSides: (double)side1: (double)side2: (double)side3;
-(double) perimeter;
-(double) height;
-(double) area;

@end
triangle.m
Code: (Objective-C)
#import "Triangle.h"
#import <math.h>

@implementation Triangle

@synthesize s1, s2, s3;

-(void) setSides: (double)side1: (double)side2: (double)side3 {
s1 = side1; //the base (90deg from height)
s2 = side2;
s3 = side3;
}

-(double) perimeter {
return s1 + s2 + s3;
}

-(double) height {
x = (s1 + s2 + s3) / 2; //x is half the sum of the sides
h = (2/s1) * sqrt(x*(x-s1)*(x-s2)*(x-s3));
return h;
}

-(double) area {
//This gets the area with the hight unknown, so the height method
//does not need to be invoked.
x = (s1 + s2 + s3) / 2;
return s1 * (2/s1) * sqrt(x*(x-s1)*(x-s2)*(x-s3)) / 2;
}
@end
Logged
sir
Full Member
***
Posts: 118


Email




« Reply #1 on: March 15, 2009, 08:08:22 PM »

oh and the circle class...
Circle.h
Code: (Objective-C)
#import "GraphicObject.h"

@interface Circle : GraphicObject {
double radius;
}

@property double radius;

-(double) area;
-(double) circumference;

@end
Circle.m
Code: (Objective-C)
#import "Circle.h"
#import <math.h>


@implementation Circle

@synthesize radius;

-(double) area {
return M_PI * (radius * radius);
}

-(double) circumference {
return 2 * M_PI * radius;
}

@end
Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #2 on: March 16, 2009, 04:50:38 AM »

Nice addition to the Triangle class!

Cheers,

Steve Kochan
Logged
Ali V
Newbie
*
Posts: 5






« Reply #3 on: March 29, 2009, 01:14:55 PM »

Its nice to see that the maths I learnt at school was actually useful  Smiley.
This is what I got for the Triangle class:
#import "Triangle.h"


@implementation Triangle
@synthesize x, y, z;

-(void) setSidesX: (double) s1 Y: (double) s2 andZ: (double) s3
{
   x = s1;
   y = s2;
   z = s3;
}
-(double) perimeter
{
   return (x + y + z);
}

-(double) area
{ //use COSINE rule and Area = 0.5 *a*b Sin C.
   // Sin^2 A = 1 - Cos^2 A.
   int CosA, area;
   CosA = ((x * x) + (y * y) - (z * z)) / (2 * x * y);
   area = 0.5 * x * y * (sqrt(1 - (CosA * CosA)));
   return area;
}
@end
Logged
Ali V
Newbie
*
Posts: 5






« Reply #4 on: March 29, 2009, 01:26:42 PM »

Sorry Line 16 should say:
double CosA, area;
Logged
brian
Newbie
*
Posts: 11



WWW Email




« Reply #5 on: May 05, 2009, 09:32:49 PM »

Interesting to see how others coded the GraphicObject class.  I'm including mine below.  The rest of my code isn't much different than Sir's code, except that in my NSLog statements for printing the colors I used %#X.  I am still wondering if there is a format character that will force the leading 0's to show up.  For color codes, I think it's important to show all 8 digits of the 32-bit code so that it's clear that the last two digits are for alpha channel.

Speaking of alpha channel, I set the alpha channel to '00' (transparent) if 'filled' is NO in both the set and get fillColor methods as you can see below.

If anyone really wants to see all of my other code, just ask and I'll post it.

GraphicObject.h:
Quote
#import <Foundation/Foundation.h>

@interface GraphicObject : NSObject
{
   int fillColor;     // 32-bit color (0x00000000 - last 2 digits are alpha channel)
   BOOL filled;       // Is the object filled?
   int lineColor;     // 32-bit line color
}

@property int lineColor;
@property BOOL filled;

-(void) setFillColor: (int) fColor andLineColor: (int) lColor;
-(void) setFillColor: (int) fColor;
-(int) getFillColor;

@end

GraphicObject.m:
Quote
#import "GraphicObject.h"


@implementation GraphicObject

@synthesize lineColor, filled;

-(void) setFillColor: (int) fColor andLineColor: (int) lColor
{
   fillColor = fColor;
   lineColor = lColor;
}

-(void) setFillColor: (int)fColor
{
   if (!filled){
      fillColor = 0x00000000;  // setting alpha channel to 00 for invisible if no fill color
   }
   else {
      fillColor = fColor;
   }
}

-(int) getFillColor
{
   if (!filled){
      return 0x00000000;  // alpha channel is 00 for invisible if no fill color
   }
   else {
      return fillColor;
   }
}

@end
Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #6 on: May 06, 2009, 04:26:06 AM »

Brian,

Use "%.8x"  to get all 8 digits displayed.   Use "%#.8x" to get a leading "0x" tacked in front (if it's a nonzero value).

Cheers,

Steve Kochan
Logged
brian
Newbie
*
Posts: 11



WWW Email




« Reply #7 on: May 06, 2009, 10:47:09 AM »

Ok, it was the dot I was forgetting.  I tried %#8X, but that only added leading spaces. 
Thanks!
Logged
zwarbo
Newbie
*
Posts: 4







« Reply #8 on: June 28, 2009, 08:39:42 PM »

I was concerned with the setting 3 sides approach, that an incorrect value for a side of a triangle could be set.
ie setSides: 2:1:0.5

So I went with a set two sides and the angle in-between option.
It uses the sin() & cos() functions however.

Code: (Objective-C)
@implementation Triangle

@synthesize side1, side2, adjacentAngle, side3;

-(void) setSide1: (double) s1 andSide2: (double) s2 andAdjacentAngle: (double) theta
{
side1 = s1;
side2 = s2;
adjacentAngle = (theta * M_PI) / 180.; // Convert theta in degrees to radians
side3 = sqrt((side1 * side1) + (side2 * side2) -
(2 * side1 * side2 * cos(adjacentAngle)));
}

-(double) area
{
double result = 0.5 * side1 * side2 * sin(adjacentAngle);

return result;
}

-(double) perimeter
{
double result = side1 + side2 + side3;

return result;
}

@end
Logged
nhohmann
Global Moderator
Jr. Member
*****
Posts: 57







« Reply #9 on: July 17, 2009, 04:16:13 AM »


-(double) area {
   //This gets the area with the hight unknown, so the height method
   //does not need to be invoked.
   x = (s1 + s2 + s3) / 2;
   return s1 * (2/s1) * sqrt(x*(x-s1)*(x-s2)*(x-s3)) / 2;
}


In the formula above, the first two appearances of s1 cancel one another out.  Likewise, the first and final 2 also cancel each other out.  That leaves you with this slightly more streamlined expression:

Code: (Objective-C)
return sqrt (x * (x - s1) * (x - s2) * (x - s3))




Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.