patterncsharpMinor
Calling a method to draw parallel lines
Viewed 0 times
methoddrawparallelcallinglines
Problem
This question is a followup to the last one.
I followed RobH's advice and his edge case bug found in my code with this solution:
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().GetLineParams(ViewType + StickLocationEnum.ToString() + linename);
Line ParallelLine1 = CameraObject.GetComponent().CreateParallelLine(referenceLine, 50);
Line ParallelLine2 = CameraObject.GetComponent().CreateParallelLine(referenceLine, -50);
Line ParallelLine3 = CameraObject.GetComponent().CreateParallelLine(referenceLine, 100);
Line ParallelLine4 = CameraObject.GetComponent().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,
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().GetLineParams(ViewType + StickLocationEnum.ToString() + linename);
Line ParallelLine1 = CameraObject.GetComponent().CreateParallelLine(referenceLine, 50);
Line ParallelLine2 = CameraObject.GetComponent().CreateParallelLine(referenceLine, -50);
Line ParallelLine3 = CameraObject.GetComponent().CreateParallelLine(referenceLine, 100);
Line ParallelLine4 = CameraObject.GetComponent().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,
Solution
How would you handle 300 parallel lines?
Something's not right isn't it?
I think you need to put words on the concepts involved here:
You need to write a method that can create
And then you can just iterate the result and write the
Line ParallelLine1 = CameraObject.GetComponent()
.CreateParallelLine(referenceLine, 50);
Line ParallelLine2 = CameraObject.GetComponent()
.CreateParallelLine(referenceLine, -50);
Line ParallelLine3 = CameraObject.GetComponent()
.CreateParallelLine(referenceLine, 100);
Line ParallelLine4 = CameraObject.GetComponent()
.CreateParallelLine(referenceLine, -100);
...
Line ParallelLine297 = CameraObject.GetComponent()
.CreateParallelLine(referenceLine, 950);
Line ParallelLine298 = CameraObject.GetComponent()
.CreateParallelLine(referenceLine, -950);
Line ParallelLine299 = CameraObject.GetComponent()
.CreateParallelLine(referenceLine, 1000);
Line ParallelLine300 = CameraObject.GetComponent()
.CreateParallelLine(referenceLine, -1000);Something's not right isn't it?
I think you need to put words on the concepts involved here:
- You have a bunch of lines.
- Lines are drawn in pairs.
- Lines are offset from the
referenceLineby a fixed interval - one line with a positive offset, the other with a negative offset.
You need to write a method that can create
n lines and return an IEnumerable given:- A
referenceLine.
- An
offsetIncrementfor the offset.
- The
nnumber of pairs of lines you want to create.
- A
LineParamsinstance.
And then you can just iterate the result and write the
AddNewLine() call only once.Code Snippets
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);
...
Line ParallelLine297 = CameraObject.GetComponent<DrawLines>()
.CreateParallelLine(referenceLine, 950);
Line ParallelLine298 = CameraObject.GetComponent<DrawLines>()
.CreateParallelLine(referenceLine, -950);
Line ParallelLine299 = CameraObject.GetComponent<DrawLines>()
.CreateParallelLine(referenceLine, 1000);
Line ParallelLine300 = CameraObject.GetComponent<DrawLines>()
.CreateParallelLine(referenceLine, -1000);Context
StackExchange Code Review Q#91417, answer score: 4
Revisions (0)
No revisions yet.