# Thread: painting math function graph

1. Member
Join Date
Feb 2011
Location
Lithuania
Posts
16
Rep Power
0

## painting math function graph

Hello,

If anybody has experienced working with edu.hws packages.

Explanation of problem:
With this package i can draw graphs of mathematical functions such as x**2, exp{x}, etc...

In math function f(x): X --> Y, where X is domain.
The problem is that domain is always (-infinity; infinity).
Can Domain be changed to [-10,10] or other

For instance it could be used drawing such function with if condition:

F(x) = (x, when x <= 0; 3*x, when 0 < x <= 3; x**2, when x>3)

Or could be given any alternatives.

Example code for drawing functions:
Java Code:
```import java.awt.Color;
import java.awt.Container;

import javax.swing.JFrame;

import edu.hws.jcm.awt.ExpressionInput;
import edu.hws.jcm.data.Function;
import edu.hws.jcm.data.Parser;
import edu.hws.jcm.data.Variable;
import edu.hws.jcm.draw.Axes;
import edu.hws.jcm.draw.CoordinateRect;
import edu.hws.jcm.draw.DisplayCanvas;
import edu.hws.jcm.draw.DrawBorder;
import edu.hws.jcm.draw.Graph1D;

public class TestGraph extends DisplayCanvas{

TestGraph(){
super();

CoordinateRect coords = new CoordinateRect(-10,10,-1,10);

Parser parser = new Parser(1023);
Variable x = new Variable("x");

ExpressionInput input = new ExpressionInput("x", parser);
Function func = input.getFunction(x);

Graph1D graph1 = new Graph1D(func);

input = new ExpressionInput("x**2", parser);
Function func1 = input.getFunction(x);

Graph1D graph2 = new Graph1D(func1);

add( new Axes("x","f(x)") );

add( new DrawBorder(Color.darkGray, 2) );

}

public static void main(String [] args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
Container con= frame.getContentPane(); // inherit main frame
con.setBackground(Color.white);        // paint background
frame.setVisible(true);
}
}```

2. What does the documentation for the library say, for this is your best resource with which to answer this and similar questions?

3. Member
Join Date
Feb 2011
Location
Lithuania
Posts
16
Rep Power
0

## Re: painting math function graph

Originally Posted by Fubarable
What does the documentation for the library say, for this is your best resource with which to answer this and similar questions?
No i did not find answer in documentation. So i was willing to find it here.

Or if you see alternatives which library should i use for paint, that help also would be helpful.

4. ## Re: painting math function graph

You could take a look at JFreeChart

db

5. Member
Join Date
Feb 2011
Location
Lithuania
Posts
16
Rep Power
0

## Re: painting math function graph

Thank you,

But that is not what I'm looking for.

6. Member
Join Date
Feb 2011
Location
Lithuania
Posts
16
Rep Power
0

## Re: painting math function graph

Ok, i finally found the answer. Library creators shared it.

Here is the answer, how to do that.

Java Code:
```import java.awt.Color;
import java.awt.Container;

import javax.swing.JFrame;

import edu.hws.jcm.awt.ExpressionInput;
import edu.hws.jcm.data.Function;
import edu.hws.jcm.data.Parser;
import edu.hws.jcm.data.Variable;
import edu.hws.jcm.draw.Axes;
import edu.hws.jcm.draw.CoordinateRect;
import edu.hws.jcm.draw.DisplayCanvas;
import edu.hws.jcm.draw.DrawBorder;
import edu.hws.jcm.draw.Graph1D;

public class TestGraph2 extends DisplayCanvas{

TestGraph2(){
super();

CoordinateRect coords = new CoordinateRect(-10,10,-1,10);

Parser parser = new Parser(1023);
Variable x = new Variable("x");

ExpressionInput input = new ExpressionInput([COLOR="#0000FF"]"0<= x and x <= 2 ? x**2"[/COLOR], parser);
Function func= input.getFunction(x);

Graph1D graph = new Graph1D(func);

add( new Axes("x","f(x)") );

add( new DrawBorder(Color.darkGray, 2) );

}

public static void main(String [] args){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
Container con= frame.getContentPane(); // inherit main frame
con.setBackground(Color.white);        // paint background
frame.setVisible(true);
}
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•