变换(transformation)
来源:互联网 发布:精易编程助手教程 编辑:程序博客网 时间:2024/06/08 12:14
运用变换
变换(transformation)修改了图形上下文中绘制图形的方式。可以通过移动、旋转或缩放实现变换。
Quartz提供了多种形式的变换,其中主要:CTM(当前变换矩阵)变换和仿射(affine)变换。
CTM(current transformation matrix)变换,这种变换比较简单,函数有:
CGContextRotateCTM,旋转坐标
CGContextScaleCTM,缩放坐标
CGContextTranslateCTM,移动原点
移动变换
CGContextTranslateCTM (myContext, 100, 50)
从对象角度沿着x轴正向移动100单位,沿着y轴正向移动50单位。
旋转变换
static inline double radians (double degrees) {return degrees * M_PI/180;}
CGContextRotateCTM (myContext, radians(–45.));
从对象角度:
在Quartz坐标下正数为逆时针旋转,负数为顺时针旋转。
在UIKit坐标下正数为顺时针旋转,负数为逆时针旋转。
缩放变换
CGContextScaleCTM (myContext, .5, .75);
从对象角度:所有x坐标缩小0.5,所有y坐标缩小0.75。
修改MyView.m文件
#import "MyView.h"@implementation MyView- (void)drawRect:(CGRect)rect { NSString *path = [[NSBundle mainBundle] pathForResource:@"cat" ofType:@"jpg"]; UIImage *img = [UIImage imageWithContentsOfFile:path]; CGImageRef image = img.CGImage; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context); CGContextRotateCTM(context, M_PI); CGContextTranslateCTM(context, -img.size.width, -img.size.height); CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height); CGContextDrawImage(context, touchRect, image); CGContextRestoreGState(context);}@end
仿射(affine)变换
仿射(affine)变换也是一种直角坐标变换,重
用变换,经过多次变换(多次的矩阵相乘),
每一种变换都可以用矩阵表示,通过多次矩阵
相乘得到最后结果。仿射变换函数:
CGAffineMakeRotation,创建旋转矩阵仿射对象
CGAffineMakeScale,创建缩放矩阵仿射对象
CGAffineMakeTranslation,创建移动矩阵仿射对象
CGAffineTransformRotate,旋转矩阵仿射对象
CGAffineTransformScale,缩放矩阵仿射对象
CGAffineTransformTranslate,移动矩阵仿射对象
CGContextConcatCTM,连接到CTM变换
使用仿射变换MyView.m
#import "MyView.h"@implementation MyView- (void)drawRect:(CGRect)rect { NSString *path = [[NSBundle mainBundle] pathForResource:@"cat" ofType:@"jpg"]; UIImage *img = [UIImage imageWithContentsOfFile:path]; CGImageRef image = img.CGImage; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context); CGAffineTransform myAffine = CGAffineTransformMakeRotation(M_PI); myAffine = CGAffineTransformTranslate(myAffine, -img.size.width, -img.size.height); CGContextConcatCTM(context, myAffine); CGContextRotateCTM(context, M_PI); CGContextTranslateCTM(context, -img.size.width, -img.size.height); CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height); CGContextDrawImage(context, touchRect, image); CGContextRestoreGState(context);}@end
- 变换(transformation)
- 等价变换(equivalent transformation)
- 刚体变换(Rigid Transformation)
- 透视变换(Perspective Transformation)
- 【OpenCV】透视变换 Perspective Transformation(续)
- Affine transformation(仿射变换)
- 【OpenCV】透视变换 Perspective Transformation(续)
- Q76:仿射变换(Affine Transformation)
- 图像几何变换(geometric transformation)
- 【OpenCV】透视变换 Perspective Transformation(续)
- 线性分式变换(linear fractional transformation)
- Vertex Transformation(顶点变换)
- Normal Transformation(法线变换)
- 透视变换 Perspective Transformation
- Normal Transformation(法线变换)
- Normal Transformation(法线变换)
- 仿射变换(Affine transformation)与python实践
- 机器学习笔记-非线性变换(Nonlinear Transformation)
- 历史上12位伟大的程序员
- 使用javascript制作俄罗斯方块游戏(1)
- 从最大似然到EM算法浅解
- 网络判断
- eclipse中关联文件设置方法
- 变换(transformation)
- Study Html《document.compatMode属性》
- 高度,能量守恒
- git 大全
- [转] Ramfs与Tmpfs的使用
- Android 常用编译命令
- XML转换为Map通用算法实现(Stax实现)
- AIX上增加逻辑卷时报错误0516-787 extendlv: Maximum allocation for logical volume jdelv02 is 512
- 程序员必须知道的几个国外IT网站