how make that more beatiful?
Gizmos.DrawLine(new Vector2(_leftBorder, _topBorder), new Vector2(_rightBorder, _topBorder));
Gizmos.DrawLine(new Vector2(_leftBorder, _topBorder), new Vector2(_leftBorder, _bottomBorder));
Gizmos.DrawLine(new Vector2(_rightBorder, _bottomBorder), new Vector2(_leftBorder, _bottomBorder));
Gizmos.DrawLine(new Vector2(_rightBorder, _bottomBorder), new Vector2(_rightBorder, _topBorder));
CodePudding user response:
Maybe you could store the vectors in fields and use a meaningful method name:
private Vector2 _topLeft = new Vector2(_leftBorder, _topBorder);
private Vector2 _topRight = new Vector2(_rightBorder, _topBorder);
private Vector2 _bottomLeft = new Vector2(_leftBorder, _bottomBorder);
private Vector2 _bottomRight = new Vector2(_rightBorder, _bottomBorder);
private void DrawRectangle()
{
Gizmos.DrawLine(_topLeft, _topRight);
Gizmos.DrawLine(_topLeft, _bottomLeft);
Gizmos.DrawLine(_bottomRight, _bottomLeft);
Gizmos.DrawLine(_bottomRight, _topRight);
}
This is at least much more readable and also more reusable.
CodePudding user response:
First things first: I find Tim's answer to be the absolutely clearest, most intuitive and reusable implementation for your use case. If I were to implement it myself, I would have followed Tim's suggestions.
Seeing as less repetition seems to be of importance to you, I'll provide a possible implementation where you first create two List<Vector2>
(one for the "left-hand vectors" and one for the "right-hand vectors" in your code example), then use a double .ForEach()
to make all the needed calls to Gizmos.DrawLine( )
:
var baseVectors = new List<Vector2>
{
new Vector2(_leftBorder, _topBorder),
new Vector2(_rightBorder, _bottomBorder)
};
var orthogonalVectors = new List<Vector2>
{
new Vector2(_rightBorder, _topBorder),
new Vector2(_leftBorder, _bottomBorder)
};
baseVectors
.ForEach(baseVector => orthogonalVectors
.ForEach(orthVector => Gizmos.DrawLine(baseVector, orthVector)));
If you decide to implement this, I would strongly advise you to find more descriptive/correct names for your lists. I couldn't come up with any better suggestions (especially as it seems to me that Vector2
is rather a point than a vector).