Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

What is the best way to refactor this method to remove redundant code? [1]: Setting new lines and adding them to a list to be drawnSetting new lines and adding them to a list to be drawn

What is the best way to refactor this method to remove redundant code? [1]: Setting new lines and adding them to a list to be drawn

What is the best way to refactor this method to remove redundant code? [1]: Setting new lines and adding them to a list to be drawn

edited title
Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

After creating parallel lines, I call Calling a method to draw the parallel lines.

Source Link
Kala J
  • 542
  • 1
  • 4
  • 17

After creating parallel lines, I call a method to draw the parallel lines.

This question is a followup to [the last one][1].

I followed RobH's advice and his edge case bug found in my code with this solution:

private Line CreateParallelLine(Line target, int offset)
{
    var parallelLine = new Line();
    var xDifference = target.point1.x - target.point2.x;
    var yDifference = target.point1.y - target.point2.y;
    var length = Math.Sqrt(Math.Pow(xDifference, 2) + Math.Pow(yDifference, 2));

    parallelLine.point1.x = (float)(target.point1.x - offset * yDifference/length);
    parallelLine.point2.x = (float)(target.point2.x - offset * yDifference/length);
    parallelLine.point1.y = (float)(target.point1.y + offset * xDifference/length);
    parallelLine.point2.y = (float)(target.point2.y + offset * xDifference/length);

    return parallelLine;
}

Now, since his solution only provided a means of creating one parallel line, even though I need to draw several lines, I have this method that draws the parallel lines (it works) but it is long and there is repeat code:

private void DrawParallelLines(object sender, LineDrawingEventArgs e)
    {
        Line referenceLine = CameraObject.GetComponent<DrawLines>().GetLineParams(ViewType + StickLocationEnum.ToString() + linename);
        Line ParallelLine1 = CameraObject.GetComponent<DrawLines>().CreateParallelLine(referenceLine, 50);
        Line ParallelLine2 = CameraObject.GetComponent<DrawLines>().CreateParallelLine(referenceLine, -50);
        Line ParallelLine3 = CameraObject.GetComponent<DrawLines>().CreateParallelLine(referenceLine, 100);
        Line ParallelLine4 = CameraObject.GetComponent<DrawLines>().CreateParallelLine(referenceLine, -100);

        LineParams paramForParallelLine = new LineParams(ViewType + "ParallelLine", Color.green, Color.green, EndPointType.Circle, EndPointType.Circle, lineThickness, endPointThickness);
        Line parallellineWithParams1 = new Line(ParallelLine1.point1, ParallelLine1.point2, paramForParallelLine, ElementType.Line);
        Line parallellineWithParams2 = new Line(ParallelLine2.point1, ParallelLine2.point2, paramForParallelLine, ElementType.Line);
        Line parallellineWithParams3 = new Line(ParallelLine3.point1, ParallelLine3.point2, paramForParallelLine, ElementType.Line);
        Line parallellineWithParams4 = new Line(ParallelLine4.point1, ParallelLine4.point2, paramForParallelLine, ElementType.Line);
        
        CameraObject.GetComponent<DrawLines>().AddNewLine(parallellineWithParams1);
        CameraObject.GetComponent<DrawLines>().AddNewLine(parallellineWithParams2);
        CameraObject.GetComponent<DrawLines>().AddNewLine(parallellineWithParams3);
        CameraObject.GetComponent<DrawLines>().AddNewLine(parallellineWithParams4);
        CameraObject.GetComponent<DrawLines>().UpdateAllLabels();
    }

What is the best way to refactor this method to remove redundant code? [1]: Setting new lines and adding them to a list to be drawn