zoukankan      html  css  js  c++  java
  • Quartz和OpenGL绘图-iphone学习日记(一)

    quartz绘图:

     

     

        CGContextRef context = UIGraphicsGetCurrentContext();//实例一个context上下文

     

     

        CGContextSetLineWidth(context, 2.0);  //设置画笔的粗细

        CGContextSetStrokeColorWithColor(context, currentColor.CGColor);//设置画笔的颜色

         CGContextSetFillColorWithColor(context, currentColor.CGColor);//设置填充色块的颜色

     


     

           // case kLineShape:

                CGContextMoveToPoint(context, firstTouch.x, firstTouch.y);//起始位置

                CGContextAddLineToPoint(context, lastTouch.x, lastTouch.y);//结束位置

                CGContextStrokePath(context);

     

     

         //   case kRectShape:

                CGContextAddRect(context, currentRect);//添加要画的currentRect

                CGContextDrawPath(context, kCGPathFillStroke);

     

           // case kEllipseShape:

                CGContextAddEllipseInRect(context, currentRect);添加要画的Ellipse

                CGContextDrawPath(context, kCGPathFillStroke);

     

     

           //case kImageShape: 

     

                CGFloat horizontalOffset = drawImage.size.width / 2;

                CGFloat verticalOffset = drawImage.size.height / 2;

                CGPoint drawPoint = CGPointMake(lastTouch.x - horizontalOffset,

                                                lastTouch.y - verticalOffset);

                [drawImage drawAtPoint:drawPoint];    //drawImage为UIImage的实例

     

     

     

     

     

     

    opengl画图:

     

     

        glLoadIdentity();//重定义虚拟机

    glClearColor(0.78f, 0.78f, 0.78f, 1.0f);//重设背景颜色

        glClear(GL_COLOR_BUFFER_BIT);

        CGColorRef color = currentColor.CGColor;//分割uicolor为4个组件

        const CGFloat *components = CGColorGetComponents(color);

        CGFloat red = components[0];

        CGFloat green = components[1];

        CGFloat blue = components[2];

        //重置color组件

        glColor4f(red,green, blue, 1.0);

     

     //case kLineShape:

                glDisable(GL_TEXTURE_2D);//设置为不要贴图效果

                GLfloat vertices[4];//浮点的数组存储x1,y1,x2,y2四个坐标

                // Convert coordinates

                vertices[0] =  firstTouch.x;

                vertices[1] = self.frame.size.height - firstTouch.y;

                vertices[2] = lastTouch.x;

                vertices[3] = self.frame.size.height - lastTouch.y;

                glLineWidth(2.0);//画笔的宽度

                glVertexPointer (2, GL_FLOAT , 0, vertices);

                glDrawArrays (GL_LINES, 0, 2);//画线

     

     

     

     

          //  case kRectShape: 

                glDisable(GL_TEXTURE_2D);

                // Calculate bounding rect and store in vertices

                GLfloat vertices[8];

                GLfloat minX = (firstTouch.x > lastTouch.x) ?

                lastTouch.x : firstTouch.x;

                GLfloat minY = (self.frame.size.height - firstTouch.y > 

                                self.frame.size.height - lastTouch.y) ? 

                self.frame.size.height - lastTouch.y : 

                self.frame.size.height - firstTouch.y;

                GLfloat maxX = (firstTouch.x > lastTouch.x) ?

                firstTouch.x : lastTouch.x;

                GLfloat maxY = (self.frame.size.height - firstTouch.y > 

                                self.frame.size.height - lastTouch.y) ? 

                self.frame.size.height - firstTouch.y : 

                self.frame.size.height - lastTouch.y;

     

                vertices[0] = maxX;

                vertices[1] = maxY;

                vertices[2] = minX;

                vertices[3] = maxY;

                vertices[4] = minX;

                vertices[5] = minY;

                vertices[6] = maxX;

                vertices[7] = minY;

     

                glVertexPointer (2, GL_FLOAT , 0, vertices);

                glDrawArrays (GL_TRIANGLE_FAN, 0, 4);///画方形

     

     

     

        //    case kEllipseShape: 

                glDisable(GL_TEXTURE_2D);

                GLfloat vertices[720];//360个点的x,y

                GLfloat xradius = (firstTouch.x > lastTouch.x) ?//计算椭圆的水平半径

                (firstTouch.x - lastTouch.x)/2 : 

                (lastTouch.x - firstTouch.x)/2;

                GLfloat yradius = (self.frame.size.height - firstTouch.y > //计算椭圆的垂直半径

                                   self.frame.size.height - lastTouch.y) ? 

                ((self.frame.size.height - firstTouch.y) - 

                 (self.frame.size.height - lastTouch.y))/2 : 

                ((self.frame.size.height - lastTouch.y) -

                 (self.frame.size.height - firstTouch.y))/2; 

                for (int i = 0; i <= 720; i+=2) { //围绕圆进行循环,计算正确的点

                    GLfloat xOffset = (firstTouch.x > lastTouch.x) ?

                    lastTouch.x + xradius 

                    : firstTouch.x + xradius;

                    GLfloat yOffset = (self.frame.size.height - firstTouch.y > 

                                       self.frame.size.height - lastTouch.y) ?

                    self.frame.size.height - lastTouch.y + yradius : 

                    self.frame.size.height - firstTouch.y + yradius;

                    vertices[i] = (cos(degreesToRadian(i))*xradius) + xOffset;

                    vertices[i+1] = (sin(degreesToRadian(i))*yradius) +

                    yOffset;

     

                }

                glVertexPointer (2, GL_FLOAT , 0, vertices);//顶点数据传给gl 并通知上下文渲染图像

                glDrawArrays (GL_TRIANGLE_FAN, 0, 360);//画圆形

     

     

     

     

     

         //   case kImageShape:

                glEnable(GL_TEXTURE_2D);

                [sprite drawAtPoint:CGPointMake(lastTouch.x, self.frame.size.height - lastTouch.y)];

    //画图片 sprite为GL_TEXTURE_2D的一个实例 需要加上GL_TEXTURE_2D.h的头文件和.m文件

     

     

     

     

    另外如何写一个很好使用的随机方法的接口呢?如下写一个随机颜色的方法:

    UIColor-Random.h//头文件

     

     

    #import <UIKit/UIKit.h>

    @interface UIColor(Random)

    +(UIColor *)randomColor;

    @end

     

     

     

    UIColor-Random.m//m文件

     

     

    #import "UIColor-Random.h"

    @implementation UIColor(Random)

    +(UIColor *)randomColor

    {

        static BOOL seeded = NO;

        if (!seeded) {

            seeded = YES;

            srandom(time(NULL));

        }

        CGFloat red =  (CGFloat)random()/(CGFloat)RAND_MAX;

        CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX;

        CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX;

        return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];

    }

    @end

     

     

    在需要用的.m文件里#import "UIColor-Random.h"就可以用(UIColor *)randomColor的这个方法了


  • 相关阅读:
    数学+高精度 ZOJ 2313 Chinese Girls' Amusement
    最短路(Bellman_Ford) POJ 1860 Currency Exchange
    贪心 Gym 100502E Opening Ceremony
    概率 Gym 100502D Dice Game
    判断 Gym 100502K Train Passengers
    BFS POJ 3278 Catch That Cow
    DFS POJ 2362 Square
    DFS ZOJ 1002/HDOJ 1045 Fire Net
    组合数学(全排列)+DFS CSU 1563 Lexicography
    stack UVA 442 Matrix Chain Multiplication
  • 原文地址:https://www.cnblogs.com/iapp/p/3631779.html
Copyright © 2011-2022 走看看