为用户创建的给定行创建行扩展 [英] Create line extensions for given line created by user

查看:61
本文介绍了为用户创建的给定行创建行扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用一个应用程序,该应用程序必须捕获用户的触摸并在屏幕上画一条线.到目前为止,这是可行的.

I've been working in an app which I have to capture user's touches and draw a line on the screen. This is working so far.

问题是我想为该行创建某种扩展,从行的开始/结束到屏幕边界.将扩展名与主线对齐很重要.

The problem is that I'd like to create some kind of extension for that line that goes from the line start/end to the screen boundaries. It's important to that extension be aligned to the main line.

几天来我一直在努力做到这一点,但没有取得积极的结果.我的想法是使用某种线性方程式表示直线,并在直线和屏幕边界上创建两个点.我遇到的第一个问题是垂直线.

I've been trying to accomplish this for some days but without positive results. My idea was to use some kind of linear equation to represent the line and after create two point lying on the line and on the screen boundaries. The first problem I faced was with vertical lines.

y = m * x + b
slope = (y2 - y2)/(x2 - x1)
y_intercept = b = y - m * x

用户也可以在任何方向和方向上创建线.

Also the user can create lines in any direction and orientation.

我试图用这些方程式找到任意点(x = 0,y = 0,x = 320,y = 480),但是我遇到了一些奇怪的问题,如下图所示.

I tried to use these equations to find arbitrary points (x = 0, y = 0, x = 320, y = 480) but I got some strange issues like in the figures bellow.

a)行数实际上超出了屏幕的限制.这导致应用程序几乎崩溃.

a) Lines going really further the limit of the screen. This caused the app almost crash.

b)另外我也无法确定如何将每个新点与当前接触点链接起来.

b) Also I couldn't identify how to link each new point with the current touch points.

代码

import Foundation
import UIKit





public class TestView : UIView
{
    var lineLayer : CAShapeLayer?
    var lineExtensionALayer : CAShapeLayer?
    var lineExtensionBLayer : CAShapeLayer?

    var startPoint : CGPoint?
    var endPoint : CGPoint?

    override init(frame: CGRect){
        super.init(frame:frame)

        self.backgroundColor = UIColor.clearColor()

        self.lineLayer = self.createLine(color: UIColor.redColor())
        self.lineExtensionALayer = self.createLine(color: UIColor.greenColor())
        self.lineExtensionBLayer = self.createLine(color: UIColor.blueColor())
    }

    required public init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    public override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        let touch : UITouch = touches.first as! UITouch
        let position = touch.locationInView(self)

        self.startPoint = position
    }

    public override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {

    }

    public override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {

    }

    public override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {

        let touch : UITouch = touches.first as! UITouch
        let position = touch.locationInView(self)

        self.endPoint = position

        self.updateLine()
    }


    func createLine(color selectedColor : UIColor) -> CAShapeLayer
    {
        let line = CAShapeLayer()
        line.frame = self.bounds
        line.strokeColor = selectedColor.CGColor
        line.fillColor = nil
        line.lineWidth = 2

        self.layer.addSublayer(line)

        return line
    }

    func drawLine(line lineToDraw : CAShapeLayer,start pointA : CGPoint, end pointB : CGPoint)
    {
        let path = UIBezierPath()
        path.moveToPoint(pointA)
        path.addLineToPoint(pointB)

        lineToDraw.path = path.CGPath
    }

    func updateLine(){

        var line = LineFunction(point1: self.startPoint!, point2: self.endPoint!)

        // Draw main line.
        self.drawLine(line: self.lineLayer!, start: self.startPoint!, end: self.endPoint!)
    }
}

推荐答案

来自

Taken from this answer, translated to swift and altered a bit:

func getLines(xmin: CGFloat, ymin: CGFloat, xmax: CGFloat, ymax: CGFloat, x1: CGFloat, x2: CGFloat, y1: CGFloat, y2: CGFloat) -> (CGPoint, CGPoint) {
    if y1 == y2 {
        return (CGPoint(x: xmin, y: y1), CGPoint(x: xmax, y: y1))
    }
    if x1 == x2 {
        return (CGPoint(x: x1, y: ymin), CGPoint(x: x1, y: ymax))
    }

    let y_for_xmin = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1)
    let y_for_xmax = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1)

    let x_for_ymin = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1)
    let x_for_ymax = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1)

    if ymin <= y_for_xmin && y_for_xmin <= ymax {
        if xmin <= x_for_ymax && x_for_ymax <= xmax {
            return (CGPoint(x: xmin, y: y_for_xmin), CGPoint(x: x_for_ymax, y: ymax))
        }
        if xmin <= x_for_ymin && x_for_ymin <= xmax {
            return (CGPoint(x: xmin, y: y_for_xmin), CGPoint(x: x_for_ymin, y: ymin))
        }
        return (CGPoint(x: xmin, y: y_for_xmin), CGPoint(x: xmax, y: y_for_xmax))
    }

    if ymin <= y_for_xmax && y_for_xmax <= ymax {
        if xmin <= x_for_ymin && x_for_ymin <= xmax {
            return (CGPoint(x: x_for_ymin, y: ymin), CGPoint(x: xmax, y: y_for_xmax))
        }
        if xmin <= x_for_ymax && x_for_ymax <= xmax {
            return (CGPoint(x: x_for_ymax, y: ymax), CGPoint(x: xmax, y: y_for_xmax))
        }
        return (CGPoint(x: xmin, y: y_for_xmin), CGPoint(x: xmax, y: y_for_xmax))
    }

    return (CGPoint(x: x_for_ymin, y: ymin), CGPoint(x: x_for_ymax, y: ymax))
}

func updateLine(){
    let x1 = self.startPoint!.x
    let x2 = self.endPoint!.x
    let y1 = self.startPoint!.y
    let y2 = self.endPoint!.y

    let (start, end) = getLines(0, ymin: 0, xmax: bounds.width, ymax: bounds.height, x1: x1, x2: x2, y1: y1, y2: y2)

    print(start, appendNewline: false)
    print(" - ", appendNewline: false)
    print(end)

    // Draw main line.
    self.drawLine(line: self.lineLayer!, start: start, end: end)
}

不幸的是,这还不能完全正常工作,因为在一半的情况下,用于返回正确的扩展行的返回的if-construct不会返回任何有用的东西.我将尝试现在或明天解决此问题.

Unfortunately that is not fully functional yet since in half of the cases the returned if-construct for returning the correct extended line is not returning anything useful. I will try to fix this either now or tomorrow.

但这应该可以帮助您开始

But it should get you started

编辑:如果起点和终点都在水平或垂直轴上,则似乎无法正常工作.如果它们位于不同的轴上,则可以正常工作.

Edit: seems to not be working if both the start and the endpoint would be on either the horizontal or vertical axis. it works if they are on different axes.

我又添加了三个return语句后,代码现在可以正常使用了:)

如果您查看记录的信息,您会发现绘制的线实际上精确地延伸到了边界,而没有延伸更多的距离.

If you take a look at the logged information you will see that the drawn line actually extends exactly to the bounds, not a bit further.

这篇关于为用户创建的给定行创建行扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆