前言
今天一时兴起,打算扔掉贝塞尔曲线,自己定义曲线,然后就做到函数上去了…
代码
package com.Diamond.canvas;
import android.view.View;
import android.content.Context;
import android.renderscript.Float2;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
public class CurveView extends View {
public Bitmap bitmap;
public CurveView(Context context, Float2 start, Float2 vec, Float2 vec2, int end_time) {
super(context);
bitmap = Bitmap.createBitmap(1080, 2160, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setARGB(255, 0, 0, 255);
canvas.drawARGB(255, 255, 255, 255);
Float2 position = start;
for (int t = 0; t < end_time; t++) {
Float2 last = new Float2(position.x, position.y);
position.x += vec.x;
position.y += vec.y;
vec.x += vec2.x;
vec.y += vec2.y;
canvas.drawLine(last.x, last.y, position.x, position.y, paint);
Log.d("" + t, position.x + "," + position.y);
}
}
public CurveView(Context context, float start, float end,float step, String xexp, String yexp) {
super(context);
bitmap = Bitmap.createBitmap(1080, 2160, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setARGB(255, 0, 0, 255);
canvas.drawARGB(255, 255, 255, 255);
Float2 position = new Float2();
position.x = anal(start,xexp);
position.y = anal(start,yexp);
for (float t = start + 1; t < end; t += step) {
Float2 last = new Float2(position.x, position.y);
position.x = anal(t,xexp);
position.y = anal(t,yexp);
canvas.drawLine(last.x,last.y,position.x,position.y,paint);
}
}
public static float anal(float t, String exp) {
float total = 0;
String[] items = exp.split("[+]+");
for (int i = 0; i < items.length; i++) {
String sub = items[i];
String number = "";
float product = 1;
for (int j = 0; j < sub.length(); j++) {
char ch = sub.charAt(j);
if (ch == 't') {
product *= t;
} else if(ch == 'r') {
product *= 1.0f / t;
} else if (ch == '*') {
if (number.length() > 0) {
float num = Float.parseFloat(number);
product *= num;
}
number = "";
} else {
number += ch;
}
}
if (number.length() > 0) {
float num = Float.parseFloat(number);
product *= num;
}
total += product;
}
return total;
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(bitmap, 0, 0, null);
super.onDraw(canvas);
}
}
示例
CurveView qf = new CurveView(this,-540,540,10,"t+540","-0.01*t*t+1080");
最初的想法
这个是我最初打算做的,用一个类似海龟(Turtle)的东西绘制,有速度以及速度增量,可以画出一些不知道怎么操作的曲线 额,第一个构造函数其实是Int2类型,被文本替换一不小心全替换掉了…
anal(analysis)
public static float anal(float t, String exp) 这个函数干掉了我许多脑细胞 功能挺简单的 变量是t,r是1/t,用于反比例函数 不支持开方什么的 示例:
anal(10,"t*t");
anal(10,"1*r");
anal(10,"t+-1");
最后再把实现给各位看看,顺便水水字数
public static float anal(float t, String exp) {
float total = 0;
String[] items = exp.split("[+]+");
for (int i = 0; i < items.length; i++) {
String sub = items[i];
String number = "";
float product = 1;
for (int j = 0; j < sub.length(); j++) {
char ch = sub.charAt(j);
if (ch == 't') {
product *= t;
} else if(ch == 'r') {
product *= 1.0f / t;
} else if (ch == '*') {
if (number.length() > 0) {
float num = Float.parseFloat(number);
product *= num;
}
number = "";
} else {
number += ch;
}
}
if (number.length() > 0) {
float num = Float.parseFloat(number);
product *= num;
}
total += product;
}
return total;
}
|