Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. package com.battlepvp.antihack.utils;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.List;
  6.  
  7. import org.bukkit.Location;
  8. import org.bukkit.util.Vector;
  9.  
  10. public class LocationIterator implements Iterator<Location> {
  11.  
  12. private List<Location> gen = new ArrayList<Location>();
  13. private int index = -1;
  14.  
  15. public LocationIterator(Location origin, int blocks) {
  16. double yaw = Math.toRadians(origin.getYaw());
  17. double pitch = Math.toRadians(origin.getPitch());
  18.  
  19. double xzProjection = Math.cos(pitch);
  20.  
  21. Vector v = new Vector();
  22. v.setX(-Math.sin(yaw) * blocks * xzProjection);
  23. v.setY(-Math.sin(pitch) * blocks);
  24. v.setZ(Math.cos(yaw) * blocks * xzProjection);
  25.  
  26. Location terminalPoint = v.toLocation(origin.getWorld()).add(origin.toVector());
  27.  
  28. double deltaX = Math.abs(origin.getX() - terminalPoint.getX());
  29. double deltaY = Math.abs(origin.getY() - terminalPoint.getY());
  30. double deltaZ = Math.abs(origin.getZ() - terminalPoint.getZ());
  31.  
  32. double maxDelta = Math.max(deltaX, Math.max(deltaY, deltaZ));
  33.  
  34. for (int n = 0; n < maxDelta; n++) {
  35. double t = n / maxDelta;
  36.  
  37. gen.add(lerp(t, origin, terminalPoint).toLocation(origin.getWorld()));
  38. }
  39. }
  40.  
  41. public LocationIterator(Location origin, Location terminalPoint) {
  42. double deltaX = Math.abs(origin.getX() - terminalPoint.getX());
  43. double deltaY = Math.abs(origin.getY() - terminalPoint.getY());
  44. double deltaZ = Math.abs(origin.getZ() - terminalPoint.getZ());
  45.  
  46. double maxDelta = Math.max(deltaX, Math.max(deltaY, deltaZ));
  47.  
  48. for (int n = 0; n < maxDelta; n++) {
  49. double t = n / maxDelta;
  50.  
  51. gen.add(lerp(t, origin, terminalPoint).toLocation(origin.getWorld()));
  52. }
  53. }
  54.  
  55. private double lerp(double t, double a, double b) {
  56. return a + t * (b - a);
  57. }
  58.  
  59. public Vector lerp(double t, Location a, Location b) {
  60. return new Vector(lerp(t, a.getX(), b.getX()), lerp(t, a.getY(), b.getY()), lerp(t, a.getZ(), b.getZ()));
  61. }
  62.  
  63. public boolean hasNext() {
  64. return index < gen.size() - 1;
  65. }
  66.  
  67. public Location next() {
  68. index++;
  69. return gen.get(index);
  70. }
  71.  
  72. public void remove() {
  73. throw new UnsupportedOperationException();
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement