Guest User

Untitled

a guest
Dec 11th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. x =[2 4 5 3 9 10]
  2. y =[-10 8 4 -5 9 10]
  3. plot(x,y,'*')
  4. [min_y,i] = min(y);
  5. pivot = [x(i),y(i)];
  6. number = i
  7. %getting the angle
  8. x(i) = [];
  9. y(i) = [];
  10. delta_x = x - ones(1,length(x))*pivot(1);
  11. delta_y = y - ones(1,length(y))*pivot(2);
  12. theta = atan2d(delta_y,delta_x);
  13. %sorting without losing the position
  14. c = [];
  15. x_new= [];
  16. y_new = [];
  17. for n = 1:length(theta)
  18. [min_theta, d] = min(theta);
  19. c = [c d];
  20. x_new = [x_new x(d)];
  21. y_new = [y_new y(d)];
  22. theta(d) = [1000]; %random value need to be higher than 180 degrees
  23. end
  24. x_new = [pivot(1) x_new pivot(1) x_new(1)];
  25. y_new = [pivot(2) y_new pivot(2) y_new(1)];
  26. %using cross product in this exercise none of the lines are collinear
  27. %If the result is 0, the points are collinear;
  28. %if it is positive, the three points constitute a "left turn" or counter-clockwise orientation,
  29. %otherwise a "right turn" or clockwise orientation (for counter-clockwise numbered points)
  30. %source wiki
  31. n_bad = [];
  32. n_good = [];
  33. x_new
  34. y_new
  35. for n = 1:(length(x_new)-3) %this caused by how the array x_new and y_new contain duplicates
  36. x_new(n+1)
  37. cross_product = (x_new(n+1) - x_new(n))*(y_new(n+2)-y_new(n))-((y_new(n+1)-y_new(n))*(x_new(n+2)-x_new(n)))
  38. if cross_product < 0
  39. n_bad = [n_bad n+1];
  40. end
  41. if cross_product > 0 %this is unnecassaray can be used for skipping making the program faster
  42. n_good = [n_good n+1]
  43. end
  44. end
  45. %in case I did something wrong the graph can be plotted here:
  46. figure
  47. x_good = [pivot(1) x_new(n_good) pivot(1)];
  48. y_good = [pivot(2) y_new(n_good) pivot(2)];
  49. plot(x_good,y_good)
  50. %to get the same index as the input
  51. if number>1
  52. n_good = n_good + 1;
  53. n = sort([n_good number]);
  54. else
  55. n = [number n_good];
  56. end
  57. n
Add Comment
Please, Sign In to add comment