「java数学函数绘图」java绘图教程
今天给各位分享java数学函数绘图的知识,其中也会对java绘图教程进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java通过Rserve调用R函数来生成图形的方法
- 2、用Applet绘制高阶函数曲线
- 3、求编写程序画出数学函数图,用java
- 4、JAVA画正弦曲线
- 5、java设计个三角函数波形绘制
- 6、java问题:需要输入数学函数公式,画出该函数的图像。现求一个公式解析的代码,或已有类库。
java通过Rserve调用R函数来生成图形的方法
你要生成什么图形?Graphics 就可以生成图片。。为什么还要
Rserve来生成图片呢?
如果真要具体代码如下
R是一个用于统计计算和统计制图的优秀工具。其功能包括:数据存储和处理系统;数组运算工具(其向量、矩阵运算方面功能尤其强大);完整连贯的统计分析工具;优秀的统计制图功能;简便而强大的编程语言:可操纵数据的输入和输入,可实现分支、循环,用户可自定义功能 。
而Java语言是目前最流行的语言,当然对我自己来说也是最熟悉的语言了。所以今天尝试通过java来调用R函数为下面通过调用数学函数实现业务功能做基础。
目前我在windows xp上做测试。
1. 首先需要下载R的windows安装程序,地址为,选择base进行下载。然后安装就可以了。
2. 安装Rserve,可以通过R界面中的命令行输入:install.packages("Rserve")或者在R界面上选择:程序包-安装程序包,然后找到Rserve进行安装。
3. 启动Rserve, 在R界面中的命令行中输入:library(Rserve)来加载Rserve,然后输入Rserve()进行启动服务。
到此Rserve已经配置并启动好,下面轮到Java程序调用了。
1. 下载Rserve提供的jar包,打开,下载REngine.jar和RserveEngine.jar,然后放到自己的项目中,并引入。
2. 编辑代码如下:
import org.rosuda.REngine.REXP;
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;
public class RTest {
/**
* @param args
* @author Zhou Rui
* @throws RserveException
* @throws REXPMismatchException
*/
public static void main(String[] args) throws RserveException, REXPMismatchException {
RConnection c = new RConnection();
REXP x = c.eval("R.version.string");
System.out.println(x.asString());
}
}
用Applet绘制高阶函数曲线
猪哥解答:
根据坐标点画图,目前没听说有人把画数学函数封装起来的,可能是没什么商业价值吧,如果你有兴趣可以自己封装一个专门做函数画图用的jar,不过工程量。。。。不敢想~
画图我会,定积分早忘干净了。我这里只收藏了一份定积分的曲线求解以及几个函数的经典画法,分享给你看看会不会对你有帮助。
一、求积分方法:
import static java.lang.Math.*;
public class DefiniteIntegral {
// 0~1区间n等分
private static int n = 100000;
// 随便定义个曲线e的x次方, 取其x在0~1的定积分;
public static double f(double x) {
double f;
f = pow(E, x);
return f;
}
// 梯形法求定积分
/**
* x0: 坐标下限, xn: 坐标上限
*/
public static double getDefiniteIntegralByTrapezium(double x0, double xn) {
double h = abs(xn - x0) / n;
double sum = 0;
for (double xi = 0; xi = xn; xi = xi + h) {
sum += (f(xi) + f(xi + h)) * h / 2;
}
return sum;
}
/**
* x0: 坐标下限, xn: 坐标上限
*/
// 矩形法求定积分, 右边界
public static double getDefiniteIntegralByRectangle1(double x0, double xn) {
//h: 步长
double h = abs(xn - x0) / n;
double sum = 0;
for (double xi = 0; xi = xn; xi = xi + h) {
sum += f(xi + h) * h;
}
return sum;
}
// 矩形法求定积分, 左边界
public static double getDefiniteIntegralByRectangle2(double x0, double xn) {
double h = abs(xn - x0) / n;
double sum = 0;
for (double xi = 0; xi = xn; xi = xi + h) {
sum += f(xi) * h;
}
return sum;
}
/**
* 测试定积分
*/
public static void main(String[] args) {
System.out.println(getDefiniteIntegralByTrapezium(0, 1));
System.out.println(getDefiniteIntegralByRectangle1(0, 1));
System.out.println(getDefiniteIntegralByRectangle2(0, 1));
}
}
输出:
1.7183090114267447
1.7183176029717737
1.7183004198816596
二、Applet画函数曲线方法(正玄曲线,我收藏的,出处找不到了)
import java.applet.*;
import java.awt.*;
public class DrawFigures extends Applet
{
int w,h,EX,EY;
public void init(){
w=Integer.parseInt(getParameter("width"));
h=Integer.parseInt(getParameter("height"));
EY=(int)((h-30)/2);
EX=(int)((w-30)/4/Math.PI);
}
final static int n=1000;
public void paint(Graphics g)
{ g.setColor(Color.green);
g.drawLine(0, h/2, w,h/2);//画出X轴
g.drawLine(w/2, 0, w/2, h);//画出Y轴
g.drawLine(w-10, h/2-10, w, h/2);
g.drawLine(w-10, h/2+10, w, h/2);//画X轴的箭头
g.drawLine(w/2, 0, w/2-10, 10);
g.drawLine(w/2, 0, w/2+10, 10);//画Y轴的箭头
int x,y,m,lenx,leny,d;
m=8; lenx=(w-30)/8;
d=8; leny=(h-30)/8;
x=w/2;
y=h/2;
double db=0.5;
int Int=1;
for(int i=0;i5;i++)
{
g.drawLine(x+i*lenx,h/2,x+i*lenx,h/2-10);
g.drawLine(x-i*lenx,h/2,x-i*lenx,h/2-10);
}
for(int i=1;i5;i++){
String s2 = String.valueOf(db); ;//double类型转换为String类型
g.drawString(s2+"π", x+i*lenx-10,h/2+20);
g.drawString(s2+"π", x-i*lenx-10,h/2+20);
db=db+0.5;
for(int q=1;q5;q++)
{
g.drawString("-", x-q*lenx-12,h/2+20);
}
}//画X轴坐标
for(int i=0;i5;i++)
{
g.drawLine(w/2,y+i*leny,w/2+10,y+leny*i);
g.drawLine(w/2,y-i*leny,w/2+10,y-leny*i);}
for(int i=1;i5;i++){
String s = Integer.toString(Int);//int类型转换为String类型
g.drawString(s,w/2+15,y-leny*i+2);
g.drawString(s,w/2+15,y+leny*i+2);
Int=Int+1;
for(int q=1;q5;q++)
{
g.drawString("-",w/2+12, y+q*leny+2);
}
}//画Y轴坐标
g.drawString("0",w/2+10,h/2+20);
g.drawString("X",w-10,h/2-20);
g.drawString("Y",w/2-20,10);
g.setColor(Color.red);
double h1=4.0*Math.PI/n;
double x1=-2*Math.PI,y1=Math.sin(x1),x2,y2;
g.setColor(Color.red);
for(int i=0;in;i++)
{
x2=x1+h1;
y2=Math.sin(x2);
g.drawLine((int)(x1*EX)+w/2,(int)(h/2)-(int)(EY*y1),(int)(x2*EX)+w/2,(int)(h/2)-(int)(y2*EY));
x1=x2;
y1=y2;
}
}
public static void main(String[] args) {
new DrawFigures();
}
}
三、附加一个经典的通用曲线处理方法-画板法,来自CSDN里elimago的博客
public class Test {
public static void main(String[] args) {
TriFunc tri = new TriFunc();
// 生成一块25×100的画布
Canvas canvas = new Canvas(25, 120);
// 画sin曲线,周期为2
tri.drawSin(canvas, 2.0);
canvas.printCanvas();
System.out.println();
canvas.reset();
// 画cos曲线,周期为2
tri.drawCos(canvas, 2.0);
canvas.printCanvas();
}
}
class TriFunc {
/**
* 画sin曲线
* @param canvas 画布
* @param period 曲线周期
*/
public void drawSin(Canvas canvas, double period) {
char[][] chars = canvas.getCanvas();
// x 轴的比率
double xRatio = (2 * period * Math.PI) / (canvas.getWidth() - 1);
// y 轴的放大倍率
int yMulti = (canvas.getHeight() - 1) / 2;
for(int i = 0; i canvas.getWidth(); i++) {
// 将数组索引映射为横坐标值
double k = (i - canvas.getWidth() / 2) * xRatio;
// 将sin值映射为数组索引
int h = yMulti - (int)Math.round(Math.sin(k) * yMulti);
chars[h][i] = Canvas.FILL_CHAR;
}
}
/**
* 画cos曲线
* @param canvas 画布
* @param period 曲线周期
*/
public void drawCos(Canvas canvas, double period) {
char[][] chars = canvas.getCanvas();
double xRatio = (2 * period * Math.PI) / (canvas.getWidth() - 1);
int yMulti = (canvas.getHeight() - 1) / 2;
for(int i = 0; i canvas.getWidth(); i++) {
double k = (i - canvas.getWidth() / 2) * xRatio;
int h = yMulti - (int)Math.round(Math.cos(k) * yMulti);
chars[h][i] = Canvas.FILL_CHAR;
}
}
}
class Canvas {
private int height;
private int width;
private char[][] canvas;
// 填充字符
public static char FILL_CHAR = '+';
// 空白字符
public static char BLANK_CHAR = ' ';
/**
* 构建一块画布
* @param height
* @param width
*/
public Canvas(int height, int width) {
// 由于需要画坐标轴,所以得采用奇数
this.height = height % 2 == 0 ? height + 1 : height;
this.width = width % 2 == 0 ? width + 1 : width;
init();
}
/**
* 初始化画布
*/
private void init() {
this.canvas = new char[height][width];
for(int i = 0; i height; i++) {
for(int j = 0; j width; j++) {
canvas[i][j] = BLANK_CHAR;
}
}
addAxis();
}
/**
* 添加坐标轴
*/
private void addAxis() {
// 添加横坐标
int y = height / 2;
for(int x = 0; x width; x++) {
canvas[y][x] = '-';
}
// 添加纵坐标
int xx = width / 2;
for(int yy = 0; yy height; yy++) {
canvas[yy][xx] = '|';
}
// 添加原点
canvas[y][xx] = '+';
}
/**
* 输出画布
*/
public void printCanvas() {
for(int i = 0; i height; i++) {
for(int j = 0; j width; j++) {
System.out.print(canvas[i][j]);
}
System.out.println();
}
}
/**
* 清空画布
*/
public void reset() {
init();
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public char[][] getCanvas() {
return canvas;
}
}
求编写程序画出数学函数图,用java
/**
* @作者 王建明
* @创建日期 2013-07-15
* @创建时间 14:25:59
* @版本号 V 1.0
*/
public class CosTest {
/**
* 画出y=cos(x)的图;br
*
* x取[0~2*pai],扩大10倍为[0,62];br
* y的范围是[-1,1],扩大10倍为[-10,10]
*/
public static void main(String[] args) {
int x = 0;
int y = 10;
/*
* 加减乘除都使用int整数类型; acos的参数依然是[-1,1]的double类型
*/
for (; y = -10; y -= 1) {// y取[-10,10]
x = (int) (Math.acos(y / 10.0) * 10);
for (int i = 0; i = 62; i++) {
if (i == x || i == 62 - x) {// x取[0,62]
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
JAVA画正弦曲线
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class SinCos extends JFrame{
private final int WIDTH = 900, HEIGHT = 450;//窗口默认的宽度、高度
private final int offsetX=15;//原点的距离窗口左边空白
private final int offsetY=5; //原点的距离窗口顶边空白
private final int stepX=200; //横向步进
private final int stepY=200; //纵向步进
private final Color colBack = Color.white; //背景颜色
private final Color colText = Color.blue; //文字标签颜色
private final Color colCros = Color.DARK_GRAY; //轴颜色
private final Color colLine = Color.red; //线颜色
private JRadioButton rdSin=new JRadioButton("Sin");
private JRadioButton rdCos=new JRadioButton("Cos");
private JButton btnDraws = new JButton("画线");
private JButton btnClear = new JButton("清空");
private boolean bDraw = false;
public void paint(Graphics g){
super.paint(g); //让父类先处理
int w=getWidth(); //窗口的宽度
int h=getHeight();//窗口的高度
g.setColor(colBack); //底色用白色
g.fillRect(0, 0, w, h); //填充整个窗口
rdSin.repaint();
rdCos.repaint();
btnDraws.repaint();
btnClear.repaint();
if(!bDraw){
return ;
}
int mid_y=(h-offsetY)/2+10;//半高
g.setFont(g.getFont().deriveFont(10f));//字体大小
g.setColor(colCros); //横线和竖线,用深灰色
g.drawLine(0, offsetY+mid_y, w, mid_y+offsetY); //横线中线
g.drawLine(offsetX, offsetY, offsetX, h); //竖线中线
g.setColor(colText); //刻度用蓝色
int maxX=(int)Math.round( (w-offsetX) / stepX);//计算一下横向最大刻度
for(int i=0;i=maxX;i++){
g.drawLine(offsetX+stepX*i, offsetY+mid_y-5, offsetX+stepX*i, offsetY+mid_y+5);//横线刻度,90步进,方便后面画线的计算
g.drawString(String.valueOf(90*i), offsetX+stepX*i-5, offsetY+mid_y+10+5); //刻度是90度
}
int maxY=(int)Math.round( mid_y / stepY);//计算一下纵向最大刻度
for(int i=1;i=maxY;i++){
g.drawLine(offsetX-5, offsetY+mid_y-stepY*i, offsetX+5, offsetY+mid_y-stepY*i);//竖线正刻度,100步进
g.drawString(String.valueOf(stepY*i), offsetX+10, offsetY+mid_y-stepY*i+5);
g.drawLine(offsetX-5, offsetY+mid_y+stepY*i, offsetX+5, offsetY+mid_y+stepY*i);//竖线负刻度,100步进
g.drawString(String.valueOf(-stepY*i), offsetX+10, offsetY+mid_y+stepY*i+5);
}
g.setColor(colLine); //曲线用红色
int x1, y1, x_=-1,y_=0;
for(int i=0; i=w; i++){ //从0度到窗口宽度,开始画Sin()点
x1=((Double)(offsetX+i/90.0*stepX) ).intValue();
if(rdSin.isSelected()){
y1=offsetY+((Double)( mid_y+stepY*Math.sin( Math.toRadians(i) )) ).intValue();
}else{
y1=offsetY+((Double)( mid_y+stepY*Math.cos( Math.toRadians(i) )) ).intValue();
}
if(x_==-1){
x_=x1;y_=y1;
}
g.drawLine(x_, y_, x1, y1);
x_=x1;y_=y1;
}
}
public SinCos(){
super("测试Graphics+Sin/Cos");
this.setSize(WIDTH, HEIGHT);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ButtonGroup group = new ButtonGroup();
group.add(rdSin);
group.add(rdCos);
this.getContentPane().add(rdSin);
this.getContentPane().add(rdCos);
this.getContentPane().add(btnDraws);
this.getContentPane().add(btnClear);
this.setVisible(true);
this.doLayout();
btnDraws.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
bDraw=true; SinCos.this.repaint();
}
});
btnClear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
bDraw=false;SinCos.this.repaint();
}
});
rdSin.setSelected(true);
}
public void doLayout(){
super.doLayout();
rdSin.setBounds(10, 15, 50, 20);
rdCos.setBounds(rdSin.getX()+rdSin.getWidth()+5, 15, 50, 20);
btnDraws.setBounds(rdCos.getX()+rdCos.getWidth()+5, 12, 70, 25);
btnClear.setBounds(btnDraws.getX()+btnDraws.getWidth()+3, 12, 70, 25);
}
public static void main(String[] args){
EventQueue.invokeLater(new Runnable() {
public void run() {
new SinCos();
}
});
}
}
java设计个三角函数波形绘制
给你个思路 java里Math是数学类 这个类里有关于三角函数的方法 具体需要什么自己查查。。要是画图形的话可以用java的绘画,画弧线来解决 毕竟三角函数也是弧线 其中的算法需要你自己算下。。这种三角函数当时学的时候就头疼 我是不能帮你解决算法。。你可以查查资料什么的
java问题:需要输入数学函数公式,画出该函数的图像。现求一个公式解析的代码,或已有类库。
我以前给j2me写的一个类,你看下:
用的时候这么用 private BDS bds=BDS.trans("-x+1");
System.out.println(bds.eval(10));
这样的
另外这里面的Hstx.pow和Hstx.ln可以用数学库里的函数替代,因为j2me里没有的
public class BDS{
public int errn=0;
public int len=0;
public int type[]=new int[100];
public double dd[]=new double[100];
public char dc[]=new char[100];
public static BDS trans(String ts){
BDS tr=new BDS();
char s[]=ts.toCharArray(),stk[]=new char[100];
int l=ts.length(),p=0,sl=0;
if(s[0]=='-')s[0]='~';
for(int i=1;il;i++)
if(s[i]=='-'(s[i-1]'0'||s[i-1]'9')s[i-1]!='x')s[i]='~';
for(int i=0;il;i++){
if(s[i]='0's[i]='9'){
double td=s[i]-'0',mt=1.0;
int hp=0;
i++;
while(il((s[i]='0's[i]='9')||s[i]=='.')){
if(s[i]=='.'){
if(hp==1){
tr.errn=1;
return tr;
}
hp=1;
}else{
if(hp==1){
mt/=10.0;
td+=(s[i]-'0')*mt;
}else td=td*10.0+(s[i]-'0');
}
i++;
}
i--;
if(hp==1mt==1.0){
tr.errn=1;
return tr;
}
tr.type[p]=1;
tr.dd[p]=td;
p++;
continue;
}
if(s[i]=='x'){
tr.type[p]=3;
p++;
continue;
}
if(s[i]=='a'){
if(i+3=l||s[i+1]!='b'||s[i+2]!='s'||s[i+3]!='('){
tr.errn=1;
return tr;
}
stk[sl++]='a';
i+=2;
continue;
}
if(s[i]=='i'){
if(i+3=l||s[i+1]!='n'||s[i+2]!='t'||s[i+3]!='('){
tr.errn=1;
return tr;
}
stk[sl++]='i';
i+=2;
continue;
}
if(s[i]=='l'){
if(i+2=l||s[i+1]!='n'||s[i+2]!='('){
tr.errn=1;
return tr;
}
stk[sl++]='l';
i++;
continue;
}
if(s[i]=='s'){
if(i+3=l||s[i+1]!='i'||s[i+2]!='n'||s[i+3]!='('){
if(i+4=l||s[i+1]!='q'||s[i+2]!='r'||s[i+3]!='t'||s[i+4]!='('){
tr.errn=1;
return tr;
}
stk[sl++]='q';
i+=3;
continue;
}
stk[sl++]='s';
i+=2;
continue;
}
if(s[i]=='c'){
if(i+3=l||s[i+1]!='o'||s[i+2]!='s'||s[i+3]!='('){
tr.errn=1;
return tr;
}
stk[sl++]='c';
i+=2;
continue;
}
if(s[i]=='t'){
if(i+3=l||s[i+1]!='a'||s[i+2]!='n'||s[i+3]!='('){
tr.errn=1;
return tr;
}
stk[sl++]='t';
i+=2;
continue;
}
if(s[i]=='~'||s[i]=='('){
stk[sl++]=s[i];
continue;
}
if(s[i]=='+'||s[i]=='-'){
while(sl0stk[sl-1]!='('){
tr.type[p]=2;
tr.dc[p]=stk[--sl];
p++;
}
stk[sl++]=s[i];
continue;
}
if(s[i]=='*'||s[i]=='/'||s[i]=='^'){
while(sl0(stk[sl-1]!='('stk[sl-1]!='+'stk[sl-1]!='-')){
tr.type[p]=2;
tr.dc[p]=stk[--sl];
p++;
}
stk[sl++]=s[i];
continue;
}
if(s[i]==')'){
while(sl0stk[sl-1]!='('){
tr.type[p]=2;
tr.dc[p]=stk[--sl];
p++;
}
sl--;
if(sl0){
tr.errn=1;
return tr;
}
continue;
}
tr.errn=1;
return tr;
}
while(sl0){
if(stk[sl-1]=='('){
tr.errn=1;
return tr;
}
tr.type[p]=2;
tr.dc[p]=stk[--sl];
p++;
}
tr.len=p;
tr.eval(1);
return tr;
}
public double eval(double x){
int sl=0;
double stk[]=new double[100];
for(int i=0;ilen;i++){
if(type[i]==1)stk[sl++]=dd[i];
else if(type[i]==3)stk[sl++]=x;
else{
if(dc[i]=='+'){
if(sl2){
errn=1;
return 0;
}
stk[sl-2]=stk[sl-2]+stk[sl-1];
sl--;
}
if(dc[i]=='-'){
if(sl2){
errn=1;
return 0;
}
stk[sl-2]=stk[sl-2]-stk[sl-1];
sl--;
}
if(dc[i]=='*'){
if(sl2){
errn=1;
return 0;
}
stk[sl-2]=stk[sl-2]*stk[sl-1];
sl--;
}
if(dc[i]=='/'){
if(sl2){
errn=1;
return 0;
}
stk[sl-2]=stk[sl-2]/stk[sl-1];
sl--;
}
if(dc[i]=='^'){
if(sl2){
errn=1;
return 0;
}
stk[sl-2]=HstxC.pow(stk[sl-2],stk[sl-1]);
sl--;
}
if(dc[i]=='~'){
if(sl1){
errn=1;
return 0;
}
stk[sl-1]=-stk[sl-1];
}
if(dc[i]=='a'){
if(sl1){
errn=1;
return 0;
}
stk[sl-1]=Math.abs(stk[sl-1]);
}
if(dc[i]=='q'){
if(sl1){
errn=1;
return 0;
}
stk[sl-1]=Math.sqrt(stk[sl-1]);
}
if(dc[i]=='i'){
if(sl1){
errn=1;
return 0;
}
stk[sl-1]=Math.floor(stk[sl-1]);
}
if(dc[i]=='s'){
if(sl1){
errn=1;
return 0;
}
stk[sl-1]=Math.sin(stk[sl-1]);
}
if(dc[i]=='c'){
if(sl1){
errn=1;
return 0;
}
stk[sl-1]=Math.cos(stk[sl-1]);
}
if(dc[i]=='t'){
if(sl1){
errn=1;
return 0;
}
stk[sl-1]=Math.tan(stk[sl-1]);
}
if(dc[i]=='l'){
if(sl1){
errn=1;
return 0;
}
stk[sl-1]=HstxC.ln(stk[sl-1]);
}
}
}
if(sl!=1){
errn=1;
return 0;
}
return stk[0];
}
}
java数学函数绘图的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java绘图教程、java数学函数绘图的信息别忘了在本站进行查找喔。