1

I want to draw a curve required output like this. But i am getting the output like this current output. Here the blue color is the background & the curve is the red colour. here My code.

for (i = 10; i <= 360; i = i + 10) {
    new_x = i;
    new_y = (float) Math.sin(new_x / 180.0 * Math.PI);

    canvas.drawLine((float) (old_x / 360.0 * w), 100 + 90 * old_y, (float) (new_x / 360.0 * w), 100 + 90 * new_y, paint);

    old_x = new_x;
    old_y = new_y;
}
Kewin Dousse
  • 3,880
  • 2
  • 25
  • 46
kashyap
  • 81
  • 1
  • 2
  • 7

1 Answers1

0

Instead of calling drawline each time, it might be more optimal to use drawPath: ie.

Path path = new Path();
boolean first = true;
for (i = 10; i <= 360; i = i + 10) {
    new_x = i;
    new_y = (float) Math.sin(new_x / 180.0 * Math.PI);


    if (first) {
        first = false;
        path.moveTo(new_x, new_y);
    }
    else{
        path.lineTo(new_x, new_y);
    }
}
canvas.drawPath(path, paint);

Then try to use quadTo instead of lineTo, for the points in the middle to smoothen it: https://developer.android.com/reference/android/graphics/Path.html#quadTo(float, float, float, float)

Nicolae Natea
  • 1,185
  • 9
  • 14