摘要 cggeometry.h檔案中封裝了一些常用的幾何方法。
目錄[-]
<a target="_blank" href="http://my.oschina.net/u/2340880/blog/406816#osc_h2_1">ios開發幾何類方法總結</a>
<a target="_blank" href="http://my.oschina.net/u/2340880/blog/406816#osc_h3_2">一、幾個常用結構體</a>
<a target="_blank" href="http://my.oschina.net/u/2340880/blog/406816#osc_h3_3">二、幾個系統定義的量</a>
<a target="_blank" href="http://my.oschina.net/u/2340880/blog/406816#osc_h3_4">三、一些常用方法</a>
cggeometry.h檔案是用c語言實作的一個封裝了許多常用幾何方法的檔案。
struct cgpoint {
cgfloat x;
cgfloat y;
};
定義一個點,設定x坐标和y坐标
struct cgsize {
cgfloat width;
cgfloat height;
定義一個尺寸,設定寬度和高度
struct cgvector {
cgfloat dx;
cgfloat dy;
定義一個二維矢量
struct cgrect {
cgpoint origin;
cgsize size;
定義一個矩形
const cgpoint cgpointzero
零點,與cgpointmake(0, 0)等效
const cgsize cgsizezero
零尺寸,與cgsizemake(0,
0)等效
const cgrect cgrectzero
零矩形,與cgrectmake(0,
0, 0, 0)等效
const cgrect cgrectnull
空矩形,這個和零矩形并不相同,當我們傳回兩個不相交矩形的交集時,會傳回空矩形。
const cgrect cgrectinfinite
無限的矩形
cgpoint cgpointmake(cgfloat x, cgfloat y);
建立一個點
cgsize cgsizemake(cgfloat width, cgfloat height);
建立一個尺寸
cgvectormake(cgfloat dx, cgfloat dy);
建立一個矢量
cgrect cgrectmake(cgfloat x, cgfloat y, cgfloat width,
cgfloat height);
建立一個矩形
cgfloat cgrectgetminx(cgrect rect);
獲得矩形最左邊的x值
cgfloat cgrectgetmidx(cgrect rect);
擷取矩形中點的x值
cgfloat cgrectgetmaxx(cgrect rect);
擷取矩形最右端的x值
cgfloat cgrectgetminy(cgrect rect);
擷取矩形最上端的y值
cgfloat cgrectgetmidy(cgrect rect);
擷取矩形中心點的y值
cgfloat cgrectgetmaxy(cgrect rect);
擷取矩形最下端的y值
cgfloat cgrectgetwidth(cgrect rect);
擷取矩形寬度
cgfloat cgrectgetheight(cgrect rect);
擷取矩形高度
bool cgpointequaltopoint(cgpoint point1, cgpoint point2);
判斷兩個點是否相等
bool cgsizeequaltosize(cgsize size1, cgsize size2);
判斷兩個尺寸是否相等
bool cgrectequaltorect(cgrect rect1, cgrect rect2);
判斷兩個矩形是否相等
cgrect cgrectstandardize(cgrect rect);
根據一個矩形建立一個标準的矩形
bool cgrectisempty(cgrect rect);
判斷是否為零矩形
cgrectisnull(cgrect rect);
判斷是否為空矩形
bool cgrectisinfinite(cgrect rect);
判斷是否為無限矩形
cgrect cgrectinset(cgrect rect, cgfloat dx, cgfloat dy);
建立一個内嵌的矩形,中心和rect參數的中心一樣,dx,dy對應内嵌的寬度和高度
比如:cgrect rect= cgrectinset(cgrectmake(0, 0, 100, 100), 10, 10);
會建立出的rect為(10,10,80,80),dx,dy也可以為負值,則是建立出來的矩形會大于原矩形範圍。
cgrect cgrectintegral(cgrect rect)
根據一個矩形,傳回四個參數都是整數的矩形
cgrect cgrectunion(cgrect r1, cgrect r2);
傳回兩個矩形的并集
cgrect cgrectintersection(cgrect r1, cgrect r2);
傳回兩個矩形的交集,如果沒有交集,傳回空矩形
cgrect cgrectoffset(cgrect rect, cgfloat dx, cgfloat dy);
傳回一個矩形,偏移量相對于rect
void cgrectdivide(cgrect rect, cgrect *slice, cgrect *remainder,
cgfloat amount, cgrectedge edge);
這個函數用來分割矩形,參數rect是源矩形,slice和remainder是分割後的兩部分矩形,amount是分割線,edge是分割選項。
注意:1、edge是一個宏,定義了分割的方式如下:
<a target="_blank" href="http://my.oschina.net/u/2340880/blog/406816#">?</a>
1
2
3
4
5
6
7
8
9
10
<code>typedef</code> <code>cf_enum(uint32_t, cgrectedge) {</code>
<code> </code><code>//從x的最小處進行垂直分割</code>
<code> </code><code>cgrectminxedge, </code>
<code> </code><code>//從y的最小處進行水準分割</code>
<code> </code><code>cgrectminyedge, </code>
<code> </code><code>//從x最大處進行垂直分割</code>
<code> </code><code>cgrectmaxxedge,</code>
<code> </code><code>//從y最大處進行水準分割</code>
<code> </code><code>cgrectmaxyedge</code>
<code>};</code>
2、slice和remainder是位址。
3、舉例如下,将會分割出兩個矩形分别為(40,0,60,100)(0,0,40,100);
<code>cgrect rect = cgrectmake(0, 0, 100, 100);</code>
<code> </code><code>cgrect slice ;</code>
<code> </code><code>cgrect remainder;</code>
<code> </code><code>cgrectdivide(rect, &slice, &remainder, 60, cgrectmaxxedge);</code>
bool cgrectcontainspoint(cgrect rect, cgpoint point);
判斷點是否在矩形内
bool cgrectcontainsrect(cgrect rect1, cgrect rect2);
判斷矩形1是否包含矩形2
bool cgrectintersectsrect(cgrect rect1, cgrect rect2);
判斷矩形1和矩形2是否相交
cfdictionaryref cgpointcreatedictionaryrepresentation(cgpoint point);
傳回一個表示點的字典
bool cgpointmakewithdictionaryrepresentation(cfdictionaryref dict,
cgpoint *point);
将字典轉換為點
cfdictionaryref cgsizecreatedictionaryrepresentation(cgsize size);
傳回一個表示尺寸的字典
bool cgsizemakewithdictionaryrepresentation(cfdictionaryref dict,
cgsize *size) ;
将字典轉換為尺寸
cfdictionaryref cgrectcreatedictionaryrepresentation(cgrect);
傳回一個表示矩形的字典
bool cgrectmakewithdictionaryrepresentation(cfdictionaryref dict,
cgrect *rect);
将字典轉化為矩形