/// <summary> /// Stores the point specified by (screenX, screenY) coordinates in the data structure.<br/> /// Determines the curve it belongs to according to the nearest last point of the curves existing.<br/> /// </summary> /// <param name="screenX"></param> /// <param name="screenY"></param> /// <param name="time">time in ms</param> public void AddInput(double screenX, double screenY, double distToWall, long time) { // transform screen coordinates double x; double y; adaptCoordinates(screenX, screenY, out x, out y); // update current position this.curX = x; this.curY = y; this.distToWall = distToWall; Boolean wasDrawing = this.isDrawing; this.isDrawing = distToWall < LIMIT_DIST_FOR_DRAWING; // check if there is a line to continue if (wasDrawing) { Wip w = curves.ElementAt(curves.Count - 1); if (time - w.time < DELTA_TIME && isPointWithinRange(x, y, w.lastPoint())) { completeLine(w, x, y, time); return; } } // new line insertNewLine(x, y, time); }
private void completeLine(Wip w, double x, double y, long time) { // TODO: move w at the top of the window if handling drawing several lines at the same time Point lastPoint = w.lastPoint(); // --- check that this is not the same point as the previous one if (lastPoint.x == x && lastPoint.y == y) { lastPoint.IncreaseSize(); return; } // --- check that X coordinate is not a change of direction if (w.direction == NO_DIRECTION) { if (x > lastPoint.x) { w.direction = LEFT_TO_RIGHT; } else { w.direction = RIGHT_TO_LEFT; } } else if ((w.direction == LEFT_TO_RIGHT && x < lastPoint.x) || (w.direction == RIGHT_TO_LEFT && x > lastPoint.x)) { // if change of direction, begin a new line insertNewLine(x, y, time); return; } // add the new point ... // --- if the three last points are aligned: remove the current last point before adding the new one Point lastBoPoint = w.lastBoPoint(); double coef1 = (double)(y - lastBoPoint.y) / (double)(x - lastBoPoint.x); double coef2 = (double)(lastPoint.y - lastBoPoint.y) / (double)(lastPoint.x - lastBoPoint.x); if (Math.Abs(1 - coef1 / coef2) < 0.05) { w.line.Remove(lastPoint); } Point newPoint = new Point(x, y); if (w.direction == LEFT_TO_RIGHT || w.direction == NO_DIRECTION) { w.line.Add(newPoint); } else { w.line.Insert(0, newPoint); } }