Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
November 25, 2017, 12:39:28 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Chapter 8 exercise 6,7,8  (Read 2320 times)
Quin
Newbie
*
Posts: 1


« on: May 21, 2016, 09:14:06 PM »

Hi everyone !
Here is my solution for exercise 6,7,8 .Some functions haven't been used.Ignore them Grin Any comments will be appreciated.

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

@interface XYPoint : NSObject

@property int x, y;

-(void) setX: (int) xVal andY: (int) yVal;

@end

#import "XYPoint.h"

@implementation XYPoint

@synthesize x, y;

-(void) setX:(int)xVal andY:(int)yVal
{
    x = xVal;
    y = yVal;
}

@end


Code: (Objective-C)
//----Rectangle class----
#import <Foundation/Foundation.h>
#import "XYPoint.h"

@interface Rectangle : NSObject

@property int width, height;

-(void) setWidth: (int) w andHeight: (int) h;
-(int) area;
-(int) perimeter;
-(void) setOrigin: (XYPoint *) pt;
-(XYPoint *) origin;
-(BOOL) containPoint: (XYPoint *) aPoint;
-(Rectangle *) intersect: (Rectangle *) intersectRect;
-(void) draw;

@end

#import "Rectangle.h"

@implementation Rectangle
{
    XYPoint * origin;
}

@synthesize width, height;

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

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

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

-(void) setOrigin:(XYPoint *)pt
{
    if (!origin) {
        origin = [[XYPoint alloc] init];
    }
   
    origin.x = pt.x;
    origin.y = pt.y;
}

-(XYPoint *) origin
{
    return origin;
}

//  determine whether the rectangle contains a point and return a Boolean value

-(BOOL) containPoint:(XYPoint *)aPoint
{
    if (( origin.x <= aPoint.x <= origin.x + width) && (origin.y <= aPoint.y <= origin.y + height)) {
        return YES;
    }
    else
        return NO;
}

//  determine wheter the rectangles intersect

-(Rectangle *) intersect:(Rectangle *)intersectRect
{
    Rectangle *commonRect = [[Rectangle alloc] init];
   
    //  discuss different situations according to the position of the parameter
   
    if (self.origin.x <= intersectRect.origin.x <= self.origin.x + self.width) {
        if (intersectRect.origin.y <= self.origin.y <= intersectRect.origin.y + intersectRect.height) {
            commonRect.origin.x = intersectRect.origin.x;
            commonRect.origin.y = self.origin.y;
            commonRect.width = self.origin.x + self.width - intersectRect.origin.x;
            commonRect.height = intersectRect.origin.y + intersectRect.height - self.origin.y;
        }
        else if (self.origin.y <= intersectRect.origin.y <= self.origin.y + self.height){
            commonRect.origin.x = intersectRect.origin.x;
            commonRect.origin.y = self.origin.y + self.height - intersectRect.origin.y;
            commonRect.width = self.origin.x + self.width - intersectRect.origin.x;
            commonRect.height = self.origin.y + self.height - intersectRect.origin.y;
        }
        else {
            commonRect.origin.x = 0;
            commonRect.origin.y = 0;
            commonRect.width = 0;
            commonRect.height = 0;
        }
        }
   
   
    else if (intersectRect.origin.x <= self.origin.x <= intersectRect.origin.x + intersectRect.width) {
        if (intersectRect.origin.y <= self.origin.y <= intersectRect.origin.y + intersectRect.height) {
            commonRect.origin.x = self.origin.x;
            commonRect.origin.y = self.origin.y;
            commonRect.width = intersectRect.origin.x + intersectRect.width - self.origin.x;
            commonRect.height = intersectRect.origin.y + intersectRect.height - self.origin.y;
        }
        else if (self.origin.y <= intersectRect.origin.y <= self.origin.y + self.height){
            commonRect.origin.x = self.origin.x;
            commonRect.origin.y = self.origin.y + self.height - intersectRect.origin.y;
            commonRect.width = intersectRect.origin.x + intersectRect.width - self.origin.x;
            commonRect.height = self.origin.y + self.height - intersectRect.origin.y;
        }
        else {
            commonRect.origin.x = 0;
            commonRect.origin.y = 0;
            commonRect.width = 0;
            commonRect.height = 0;
        }
    }
   
   
    else {
        commonRect.origin.x = 0;
        commonRect.origin.y = 0;
        commonRect.width = 0;
        commonRect.height = 0;
    }

    return commonRect;
   
}
-(void) draw
{
    int i, j;
   
    // print as many "-" as the width of the rectangle
   
    for (i = 0; i <= self.width; i ++) {
        printf("-");
    }
   
    //  print as many "|       |" as the height of the rectangle
   
    printf("\n");
    for (j = 0; j <= self.height; j ++) {
        printf("|         |\n");
    }
   
    for (i = 0; i <= self.width; i ++) {
        printf("-");
    }
    printf("\n");
}

@end

Code: (Objective-C)
//-- program part---
#import "Rectangle.h"
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Rectangle *aRect = [[Rectangle alloc] init];
        Rectangle *bRect = [[Rectangle alloc] init];
        Rectangle *commonRect = [[Rectangle alloc] init];
       
        XYPoint *aPoint = [[XYPoint alloc] init];
        XYPoint *bPoint = [[XYPoint alloc] init];
        XYPoint *cPoint = [[XYPoint alloc] init];
       
        [aRect setWidth:25 andHeight:7];
        [bRect setWidth:10 andHeight:18];
       
        [aPoint setX:20 andY:42];
        [bPoint setX:40 andY:30];
        [cPoint setX:25 andY:43];
       
        commonRect = [aRect intersect:bRect];
       
        if (commonRect.width) {
            printf("The rectangles intersect and the graph below shows the common part :\n");
            [commonRect draw];
            printf("The width of the common part is %i\n", commonRect.width);
        }
        else
            printf("The rectangle don't intersect.\n");
       
        if ([aRect containPoint:cPoint]) {
            printf("The rectangle contains the point (%i,%i).\n", cPoint.x, cPoint.y);
        }
        else
            printf("The rectangle don't contain the point.\n");
       
    }
    return 0;
}

Here is the output:

The rectangles intersect and the graph below shows the common part :
--------------------------
|         |
|         |
|         |
|         |
|         |
|         |
|         |
|         |
|         |
|         |
|         |
|         |
|         |
|         |
|         |
|         |
|         |
|         |
|         |
--------------------------
The width of the common part is 25
The rectangle contains the point (25,43).
Program ended with exit code: 0
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

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 ゥ 2009 classroomM.com. All rights reserved.