Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class V extends View {
- Paint paint = new Paint();
- Random rnd = new Random();
- CatmullRomSpline spline;
- public V(Context context) {
- super(context);
- paint.setColor(Color.GREEN);
- paint.setStrokeWidth(5);
- paint.setStyle(Paint.Style.STROKE);
- paint.setTextSize(64);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- canvas.drawText("Click me", 100, 100, paint);
- int R = 300;
- int NUM_EDGES = 20;
- int NUM_POINTS = 100;
- canvas.translate(getWidth() / 2, getHeight() / 2);
- float[] arr = new float[NUM_EDGES];
- for (int i = 0; i < arr.length; i++) {
- arr[i] = R / 30 * (float) (rnd.nextGaussian());
- }
- spline = new CatmullRomSpline(arr);
- float x0 = Float.NEGATIVE_INFINITY;
- float y0 = Float.NEGATIVE_INFINITY;
- for (int i = 0; i <= NUM_POINTS; i++) {
- double a = i * 2 * Math.PI / NUM_POINTS;
- float r = R + spline.q(i / (float) NUM_POINTS);
- float x = r * (float) Math.sin(a);
- float y = r * (float) Math.cos(a);
- if (x0 != Float.NEGATIVE_INFINITY) {
- canvas.drawLine(x, y, x0, y0, paint);
- }
- x0 = x;
- y0 = y;
- }
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- invalidate();
- return true;
- }
- }
- // sample output is http://ctrlv.in/524966 or http://ctrlv.in/524967
- public class CatmullRomSpline {
- float[] points;
- public CatmullRomSpline(float[] points) {
- this.points = points;
- }
- public float q(float t) {
- int i = (int) (t * points.length);
- t = t * points.length - i;
- return _q(t, p(i-1), p(i), p(i+1), p(i+2));
- }
- private float p(int i) {
- i = (i + points.length) % points.length;
- return points[i];
- }
- private float _q(float t, float p0, float p1, float p2, float p3) {
- return 0.5f * ((2 * p1) +
- (p2 - p0) * t +
- (2 * p0 - 5 * p1 + 4 * p2 - p3) * t * t +
- (3 * p1 -p0 - 3 * p2 + p3) * t * t * t);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement