기본 콘텐츠로 건너뛰기

3월, 2015의 게시물 표시

objc, draw simple line on UIView

- ( void )drawRect:( CGRect )rect {     // Drawing code     CGContextRef context = UIGraphicsGetCurrentContext();  // コンテキストを取得       // 線を黒色にする     CGContextSetStrokeColorWithColor(context,UIColor.blackColor.CGColor);       // 線の太さを指定     CGContextSetLineWidth(context, 10.0);  // 10ptに設定       CGContextMoveToPoint(context, 50, 100);  // 始点     CGContextAddLineToPoint(context, 100, 200);     CGContextAddLineToPoint(context, 200, 200);     CGContextAddLineToPoint(context, 300, 200); // 終点     CGContextStrokePath(context);  // 描画 } //refresh     [ self setNeedsDisplay ]; ref http://d.hatena.ne.jp/kazukingband/20120713/1342176398

c, get distance between a point and a line (two points)

float calcDistance( float x1, float y1, float x2, float y2, float x3, float y3) {      float px = x2-x1;     float py = y2-y1;          float something = px*px + py*py;          float u =  ((x3 - x1) * px + (y3 - y1) * py) / something;          if (u > 1 ){         u = 1 ;     } else if (u < 0 )     {         u = 0 ;     }      float x = x1 + u * px;     float y = y1 + u * py;          float dx = x - x3;     float dy = y - y3;               float dist = sqrt (dx*dx + dy*dy);              return dist; } http://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment