Skip to content

Instantly share code, notes, and snippets.

@olegknyazev
Created October 28, 2018 14:59
Show Gist options
  • Save olegknyazev/f46c38be91b2929249b457c789dfa6f3 to your computer and use it in GitHub Desktop.
Save olegknyazev/f46c38be91b2929249b457c789dfa6f3 to your computer and use it in GitHub Desktop.
About Formatting: Writing dense code
public Vector3 WallNormal(Vector3 position, bool smoothNormalAtCorners = false) {
var clampedPosition = Clamp(position);
var positions =
_alignmentArea
.Where(path => !path.isEmpty)
.Select(path => {
int nearestSegIndex;
Vector2 nearestPoint2D;
var side = path.PointSide(Project(position), out nearestPoint2D, out nearestSegIndex);
var nearestPoint3D = Unproject(nearestPoint2D);
var segDir = path.Segment(nearestSegIndex).direction;
return new {
nearestPoint = nearestPoint3D,
distance = (clampedPosition - nearestPoint3D).magnitude,
normal = Unproject(new Vector2(-segDir.y, segDir.x)),
side = side
};
});
if (!positions.Any())
return Vector3.zero;
var result = positions.MinBy(x => x.distance);
var pointOnBounds = result.nearestPoint;
return
smoothNormalAtCorners
? (clampedPosition - pointOnBounds).normalized * result.side
: result.normal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment