Last active
August 29, 2015 13:56
-
-
Save novnan/9021008 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 绘制几何体的形状,省略绘图代码 | |
// main.m | |
// Shape-Procedural | |
// | |
// Created by Novnan on 14-2-15. | |
// Copyright (c) 2014年 Novnan. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
typedef enum { //定义几种不同的形状 | |
kCircle, | |
kRectangle, | |
kEgg } 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[], int); | |
NSString *colorName (ShapeColor); | |
int main(int argc, const char * argv[]) | |
{ // 声明要绘制的形状数组 | |
Shape shapes[3]; | |
ShapeRect rect0 = { 0 , 0, 10, 30 }; | |
shapes[0].type = kCircle; | |
shapes[0].fillColor = kRedColor; | |
shapes[0].bounds = rect0; | |
ShapeRect rect1 = { 30, 40, 50, 60 }; | |
shapes[1].type = kRectangle; | |
shapes[1].fillColor = kGreenColor; | |
shapes[1].bounds = rect1; | |
ShapeRect rect2 = { 15, 18, 27, 29 }; | |
shapes[2].type = kEgg; | |
shapes[2].fillColor = kBlueColor; | |
shapes[2].bounds = rect2; | |
drawShapes (shapes, 3); | |
return (0); | |
} //main函数调用了drawShapes函数来绘制形状 | |
void drawShapes (Shape shapes[], int count) | |
{ | |
for (int 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 kEgg: | |
DrawEgg (shapes[i].bounds, shapes[i].fillColor); | |
break; | |
} | |
} | |
} | |
void drawCirlce (ShapeRect, ShapeColor); | |
//下面是drawCircle()函数的代码,此函数输出矩形区域信息的传递给它的颜色 | |
void drawCirlce (ShapeRect bounds, ShapeColor fillcolor) | |
{ | |
NSLog(@"drawing a circle at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, | |
colorName(fillcolor)); | |
} | |
// NSlog()中调用的colorName()函数负责转换传入的颜色值,并返回NSString字面量,比如@"red" 或 @"bule" | |
NSString *colorName (ShapeColor colorName) | |
{ | |
switch (colorName) { | |
case kRedColor: | |
return @"red"; | |
break; | |
case kGreenColor: | |
return @"green"; | |
break; | |
case kBlueColor: | |
return @"blue"; | |
break; | |
} | |
return @"no clue"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment