Skip to content

Instantly share code, notes, and snippets.

@Alwinfy
Last active April 23, 2020 15:22
Show Gist options
  • Save Alwinfy/6c11d6dd554d113caad256edd0df6c2e to your computer and use it in GitHub Desktop.
Save Alwinfy/6c11d6dd554d113caad256edd0df6c2e to your computer and use it in GitHub Desktop.
Entity closestToRay(Vector3 a, Vector3 b, Iterable<Entity> list) {
if(a.equals(b)) return closestToPoint(a, list);
Vec3d start = a.toVec3D();
Vec3d offset = b.toVec3D().subtract(start);
Vec3d diff = offset.normalize();
double minDot = diff.dotProduct(offset);
double minDist = Double.MAX_VALUE;
Entity found = null;
for(Entity e : list) {
Vec3d pos = entity.getPositionVector().subtract(start);
double dot = diff.dotProduct(pos);
double dist;
if(dot <= minDot) dist = pos.length();
else dist = pos.crossProduct(diff).length();
if(dist < minDist) {
minDist = dist;
found = e;
}
}
if(found == null)
throw new RuntimeException("fuck");
return found;
}
Entity closestToLineSegment(Vector3 a, Vector3 b, Iterable<Entity> list) {
if(a.equals(b)) return closestToPoint(a, list);
Vec3d start = a.toVec3D();
Vec3d end = b.toVec3D();
Vec3d diff = end.subtract(start).normalize();
double minDot = diff.dotProduct(start);
double maxDot = diff.dotProduct(end);
double minDist = Double.MAX_VALUE;
Entity found = null;
for(Entity e : list) {
Vec3d pos = entity.getPositionVector();
double dot = diff.dotProduct(pos);
double dist;
if(dot <= minDot) dist = pos.subtract(start).length();
else if(dot >= maxDot) dist = pos.subtract(end).length();
else dist = pos.subtract(start).crossProduct(diff).length();
if(dist < minDist) {
minDist = dist;
found = e;
}
}
if(found == null)
throw new RuntimeException("fuck");
return found;
}
Entity closestToLine(Vector3 a, Vector3 b, Iterable<Entity> list) {
if(a.equals(b)) return closestToPoint(a, list);
Vec3d start = a.toVec3D();
Vec3d diff = b.toVec3D().subtract(start).normalize();
double minDist = Double.MAX_VALUE;
Entity found = null;
for(Entity e : list) {
Vec3d pos = entity.getPositionVector();
double dist = pos.subtract(start).crossProduct(diff).length();
if(dist < minDist) {
minDist = dist;
found = e;
}
}
if(found == null)
throw new RuntimeException("fuck");
return found;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment