Advertisement
Guest User

Untitled

a guest
Mar 26th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. class V extends View {
  2. Paint paint = new Paint();
  3. Random rnd = new Random();
  4. CatmullRomSpline spline;
  5.  
  6. public V(Context context) {
  7. super(context);
  8. paint.setColor(Color.GREEN);
  9. paint.setStrokeWidth(5);
  10. paint.setStyle(Paint.Style.STROKE);
  11. paint.setTextSize(64);
  12. }
  13.  
  14. @Override
  15. protected void onDraw(Canvas canvas) {
  16. canvas.drawText("Click me", 100, 100, paint);
  17.  
  18. int R = 300;
  19. int NUM_EDGES = 20;
  20. int NUM_POINTS = 100;
  21.  
  22. canvas.translate(getWidth() / 2, getHeight() / 2);
  23. float[] arr = new float[NUM_EDGES];
  24. for (int i = 0; i < arr.length; i++) {
  25. arr[i] = R / 30 * (float) (rnd.nextGaussian());
  26. }
  27. spline = new CatmullRomSpline(arr);
  28. float x0 = Float.NEGATIVE_INFINITY;
  29. float y0 = Float.NEGATIVE_INFINITY;
  30. for (int i = 0; i <= NUM_POINTS; i++) {
  31. double a = i * 2 * Math.PI / NUM_POINTS;
  32. float r = R + spline.q(i / (float) NUM_POINTS);
  33. float x = r * (float) Math.sin(a);
  34. float y = r * (float) Math.cos(a);
  35. if (x0 != Float.NEGATIVE_INFINITY) {
  36. canvas.drawLine(x, y, x0, y0, paint);
  37. }
  38. x0 = x;
  39. y0 = y;
  40. }
  41. }
  42.  
  43. @Override
  44. public boolean onTouchEvent(MotionEvent event) {
  45. invalidate();
  46. return true;
  47. }
  48. }
  49.  
  50. // sample output is http://ctrlv.in/524966 or http://ctrlv.in/524967
  51. public class CatmullRomSpline {
  52. float[] points;
  53.  
  54. public CatmullRomSpline(float[] points) {
  55. this.points = points;
  56. }
  57.  
  58. public float q(float t) {
  59. int i = (int) (t * points.length);
  60. t = t * points.length - i;
  61. return _q(t, p(i-1), p(i), p(i+1), p(i+2));
  62. }
  63.  
  64. private float p(int i) {
  65. i = (i + points.length) % points.length;
  66. return points[i];
  67. }
  68.  
  69. private float _q(float t, float p0, float p1, float p2, float p3) {
  70. return 0.5f * ((2 * p1) +
  71. (p2 - p0) * t +
  72. (2 * p0 - 5 * p1 + 4 * p2 - p3) * t * t +
  73. (3 * p1 -p0 - 3 * p2 + p3) * t * t * t);
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement