obj c 画 图形 过程式开发

本文探讨了使用过程式语言进行开发时所面临的复杂性和挑战,通过具体代码实例展示了如何定义不同形状类型和颜色,并实现基本的绘制功能。

再次“复习”下过程式语言的繁琐

 

//
//  main.m
//  shapes-Procedural
//
//  Created by Wunderman on 11-12-28.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//  过程式开发
//

#import <Foundation/Foundation.h>



typedef enum {
    kCircle,
    kRectangle,
    kOblateSpheroid
} ShapeType;

typedef enum {
    kRedColor,
    kGreenColor,
    kBlueColor
} ShapeColor;

typedef struct {
    int x, y, width, height;
} ShapeRect;

typedef struct {
    ShapeType type;
    ShapeColor fillColor;
    ShapeRect bounds;
} Shape;

void drawShapes(Shape shapes[], int count);
void drawCircle(ShapeRect bounts, ShapeColor fillColor);
NSString *colorName(ShapeColor colorName);

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        
        // insert code here...
        NSLog(@"Hello, World!");
        
        Shape shape[3];
        
        ShapeRect bounds_0 = {0, 0, 10, 30};
        shape[0].type = kCircle;
        shape[0].fillColor = kRedColor;
        shape[0].bounds = bounds_0;
        
        ShapeRect bounds_1 = {1, 1, 11, 31};
        shape[1].type = kRectangle;
        shape[1].fillColor = kGreenColor;
        shape[1].bounds = bounds_1;

        
        ShapeRect bounds_2 = {2, 2, 12, 32};
        shape[2].type = kOblateSpheroid;
        shape[2].fillColor = kBlueColor;
        shape[2].bounds = bounds_2;
        
        drawShapes(shape, 3);
        
        }
    
    return 0;
}


void drawShapes(Shape shapes[], int count) {
    int i;
    for(i = 0; i<count; i++) {
        switch (shapes[i].type) {
            case kCircle:
                drawCircle(shapes[i].bounds, shapes[i].fillColor);
                break;
                
            case kRectangle:
                //drawRectangle(shapes[i].bounds, shapes[i].fillColor);
                break;
                
            case kOblateSpheroid:
                //drawOblateSpheroid(shapes[i].bounds, shapes[i].fillColor);
                break;
                
            default:
                break;
        }
    }
}

void drawCircle(ShapeRect bounts, ShapeColor fillColor) {
    NSLog(@"drawing a circle at (%d, %d, %d, %d) in %@", bounts.x, bounts.y, bounts.width, bounts.height, colorName(fillColor));
}

NSString *colorName(ShapeColor colorName) {
    switch (colorName) {
        case kRedColor:
            return @"red";
            break;
            
        case kGreenColor:
            return @"green";
            break;
            
        case kBlueColor:
            return @"blue";
            break;
            
        default:
            break;
    }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值