第八章继承课后练习答案

本文介绍了一个基于Objective-C的图形类库实现细节,包括矩形、圆形、三角形等基本图形类的设计与方法实现。重点展示了如何创建图形对象、计算面积与周长等属性,并实现了图形对象间的交互,如矩形交集计算。
<strong><span style="font-size:24px;">没有一个一个分开写  大家看的时候 看.h文件和.m文件可以分辨出来;</span></strong>
</pre><pre name="code" class="html">#import <Foundation/Foundation.h>

@interface XYpoint : NSObject

@property float x,y;
-(void) setX:(float)xVal andY:(float)yVal;
-(void) printf;
@end
#import "XYpoint.h"

@implementation XYpoint

-(void) setX:(float)xVal andY:(float)yVal;
{
    
    _x=xVal;
    _y=yVal;
}
-(void) printf
{
    NSLog(@"----(%.1f,%.1f)",_x,_y);
}
@end
//  Rectangle.h


#import <Foundation/Foundation.h>
//@class XYpoint;
#import "XYpoint.h"
#import "GraphicObject.h"
@interface Rectangle : GraphicObject

@property float width,height;
@property XYpoint *origin;

-(void) initwidth:(float)width height:(float)height origin:(XYpoint *)origin;
-(void) print;
-(float) area;//矩形面积
-(float) perimeter;//矩形周长
-(void) setWidth:(float)w andheight:(float)h;
-(void) translate;//使用XYPoint对象作为其参数,通过制定的向量对矩形的原点进行变换
-(BOOL) constainsPoint:(XYpoint *)aPoint;//191页第6题
-(Rectangle *) intersect:(Rectangle *)arect;//191页第7题
@end

//
//  Rectangle.m



#import "Rectangle.h"

#import "XYpoint.h"
@implementation Rectangle

-(void) initwidth:(float)width height:(float)height origin:(XYpoint *)origin
{
    _width=width;
    _height=height;
    _origin=origin;
}
-(void) print
{
    NSLog(@"---width=%.1f---height=%.1f",_width,_height);

}
-(float) area;
{
    return _width * _height;
}
-(float) perimeter;
{
    return (_width+_height)*2;
}
-(void) setWidth:(float)w andheight:(float)h;
{
    _width=w;
    _height=h;
}

-(void) translate
{
    _origin.x+=2;
    _origin.y+=3;
}
-(BOOL) constainsPoint:(XYpoint *)aPoint
{
    if (aPoint.x>=_origin.x && aPoint.x<=_origin.x+_width &&
        aPoint.y>=_origin.y && aPoint.y<=_origin.x+_height)
    {
        return YES;
    }
    else
        return NO;
}
-(Rectangle *) intersect:(Rectangle *)aRect
{
    Rectangle *crossRect=[[Rectangle alloc]init];
    XYpoint *point1=[[XYpoint alloc]init];
    
    if (fabsf(aRect.origin.x+ aRect.width/2- _origin.x - _width/2) <= _width/2 + aRect.width/2 && fabsf(aRect.origin.y+ aRect.height/2- _origin.y - _height/2) <= _height/2+aRect.height/2)
    {
        float Xc1,Yc1;
        float Xc2,Yc2;
        if (_origin.x > aRect.origin.x)
            Xc1=_origin.x;
        else
            Xc1=aRect.origin.x;
        if (_origin.y > aRect.origin.y)
            Yc1=_origin.y;
        else
            Yc1=aRect.origin.y;
        if (_origin.x+_width < aRect.origin.x+aRect.width)
            Xc2=_origin.x+_width;
        else
            Xc2=aRect.origin.x+aRect.width;
        if (_origin.y+_height < aRect.origin.y+aRect.height)
            Yc2=_origin.y+_height;
        else
            Yc2=aRect.origin.y+aRect.height;
        NSLog(@"--(%.1f,%.1f) --(%.1f,%.1f)",Xc1,Yc1,Xc2,Yc2);
        
        if (Xc1 < Xc2 && Yc1 < Yc2)
        {
            [point1 setX:Xc1 andY:Yc1];
            [point1 printf];
            
            [crossRect initwidth:Xc2-Xc1 height:Yc2-Yc1 origin:point1];
            [crossRect print];
        }
        else
        {
            [point1 setX:0 andY:0];
            [point1 printf];
            [crossRect initwidth:0 height:0 origin:point1];
            [crossRect print];
        }
    }
    else
        NSLog(@"NO cross");
    return crossRect;
    
}
@end

//
//  GraphicObject.h


#import <Foundation/Foundation.h>

@interface GraphicObject : NSObject


@property int fillColor,lineColor;

//    int fillColor;//32位颜色
//    BOOL filled;//是否为对象填充了?
//    int lineColor;//32位线的颜色

-(void) setfillcolor:(int)m andfilled:(BOOL)l andlinecolor:(int)n;
@end
//
//  GraphicObject.m


#import "GraphicObject.h"

@implementation GraphicObject

-(void) setfillcolor:(int)m andfilled:(BOOL)l andlinecolor:(int)n
{
    if (l) {
        _fillColor=m;
        _lineColor=n;
    }
}
@end

//
//  Circle.h
/

#import <Foundation/Foundation.h>
#import "GraphicObject.h"
@interface Circle : GraphicObject
@property int r;
-(int) circleArea;
-(int) circlePerimeter;
//-(void) setR:(int)circleR;
@end


//
//  Circle.m
/

#import "Circle.h"

@implementation Circle

-(int) circleArea
{
    return 3.14*_r*_r;
}
-(int) circlePerimeter;
{
    return 2*3.14*_r;
}
//-(void) setR:(int) circleR
//{
//    _r=circleR;
//}
@end

//
//  Triangle.h
//  XYpoint
//
//  Created by  on 15/4/14.
//  Copyright (c) 2015年  All rights reserved.
//

#import <Foundation/Foundation.h>
#import "GraphicObject.h"
@interface Triangle : GraphicObject
@property double a,b,c;
-(double) TriArea;
-(double) TriPerimeter;
-(void) anda:(double)aValue andb:(double)bValue andc:(double)cValue;
@end
//
//  Triangle.m
//  XYpoint
//
//  Created by  on 15/4/14.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "Triangle.h"

@implementation Triangle
@synthesize a,b,c;
-(double) TriArea//用海伦公式求
{
    int s;
    s=1.0/2*(a+b+c);
    return sqrt(s*(s-a)*(s-b)*(s-c));
}
-(double) TriPerimeter
{
    return a+b+c;
}
-(void) anda:(double)aValue andb:(double)bValue andc:(double)cValue
{
    a=aValue;
    b=bValue;
    c=cValue;
}
@end
</pre><pre name="code" class="html">//
//  main.m
//  XYpoint
//
//  Created by new on 15/4/10.
//  Copyright (c) 2015年 new. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Rectangle.h"
#import "XYpoint.h"
#import "GraphicObject.h"
#import "Circle.h"
#import "Triangle.h"
int main(int argc, const char * argv[]) {
//    @autoreleasepool {
//        Rectangle *myRect=[[Rectangle alloc]init];
//        XYpoint *myPoint=[[XYpoint alloc]init];
//        GraphicObject *myGrap=[[GraphicObject alloc]init];
//        Circle *myCircle=[[Circle alloc]init];
//        Triangle *myTri=[[Triangle alloc]init];
//        XYpoint *aPoint=[[XYpoint alloc]init];
//        
//        [myPoint setX: 100 andY:200];
//        [myRect  setWidth:5 andheight:8];
//        
//        myRect.origin=myPoint;
//        
//       
//        [myGrap setfillcolor:31 andfilled:YES andlinecolor:30];
//        [myCircle setR:6];
//        [myTri anda:3 andb:4 andc:5];
//        
//        NSLog(@"Rectangle w=%.1f,h=%.1f",[myRect width],myRect.height);
//        NSLog(@"area=%.1f,perimeter=%.1f", [myRect area],[myRect perimeter]);
//        NSLog(@"origin at (%.1f %.1f)",myRect.origin.x,myRect.origin.y);
//        [myPoint setX: 50 andY:60];
//        
//        NSLog(@"origin at (%.1f %.1f)",myRect.origin.x,myRect.origin.y);
//        [myRect translate];
//        NSLog(@"origin at (%.1f %.1f)",myRect.origin.x,myRect.origin.y);
//        
//        NSLog(@"cirarea=%i,cirpert=%i", [myCircle circleArea],[myCircle circlePerimeter]);
//        NSLog(@"tirarea=%.1f,tirpert=%.1f", [myTri TriArea],[myTri TriPerimeter]);
//        NSLog(@"fillcolor is %i,linecolor %i",myGrap.fillColor,myGrap.lineColor);
//        
//        [aPoint setX:103 andY:206];
//        myRect.origin=aPoint;
//       
//        NSLog(@"%@",aPoint?@"yes":@"");
//    }
    以上说前六道题
//以下是第七题,方便看清
    @autoreleasepool {
        XYpoint *firstPoint=[[XYpoint alloc]init];
        XYpoint *secondPoint=[[XYpoint alloc]init];
        Rectangle *firstRect=[[Rectangle alloc] init];
        Rectangle *secondRect=[[Rectangle alloc] init];
        
        [firstPoint setX:123 andY:420];
        [firstRect initwidth:250 height:75 origin:firstPoint];
        [secondPoint setX:56 andY:80];
        [secondRect initwidth:100 height:180 origin:secondPoint];
        [firstRect print];
        [firstPoint printf];
        [secondRect print];
        [secondPoint printf];
        
        Rectangle *threeRect=[firstRect intersect:secondRect];

        NSLog(@"__area=%.1f,__per=%.1f",[threeRect area],[threeRect perimeter]);
    }
    return 0;
}

-(void) draw
{
    int i;
    int j;
    for (i=0; i<_width; i++)
        printf("-");
        printf("\n");
    for (j=0; j<_height; j++) {
        printf("|");
    for (i=0; i<_width;i++ ) {
            printf(" ");
        }
            printf("|\n");
        }
    for (i=0; i<_width; i++)
        printf("-");
    printf("\n");
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值