Urgent! How to display this function,use symbol"*" to draw line


Here teacher gave one method to perform this function

public static long evaluations;

public static double func(double x)
{
double result;

result = Math.abs( Math.sin(0.017453292519943295769236907684886*x) * (x/90) );

evaluations++;

return result;
}
}

question is how to use this method to draw this diagram

picture:
http://simpleface.files.wordpress.com/2011/04/screen-shot-2011-04-29-at-9-41-54-am.png

Menu:
The menu has the following 3 options:
[1] Display function
[2] Find x-value
[3] Exit
Option 1 lets the user roughly visualise the function on the terminal/console screen.
Option 2 lets the user search for a particular input value at which a specified output
value can be achieved. (See below for full descriptions of these options.) The program
can only be finished when the user selects option 3. Erroneous input should also not
be accepted at any stage.
Display function option:
When this option is selected, the user is allowed to enter an x value range and an
output width and a height. The program should then ensure that the portion of the
function that is chosen (through the x value range) correctly fits the specified
rectangular width and height.

Dani AI

Generated

A practical way to satisfy Option 1 is to treat the console as a width×height pixel grid: sample the given x-range, find the function's min/max over that range, scale y into row indices (top = row 0), and place a '*' at each column's computed row. This uses the existing func(double) evaluator from 's post and keeps the terminal output inside the requested rectangle.

Steps:

  1. Sample the x-range (one sample per column is minimal; use extra samples per column to compute a stable y-min/y-max).
  2. If y-min == y-max, expand the range slightly so scaling works.
  3. Create a char[height][width] filled with spaces; for each column compute x and y, map y → row and set '*'.
  4. Optionally draw horizontal/vertical axes when 0 lies in range (use '-', '|' or '+' at intersections).
  5. Print rows top-to-bottom.

Example (drop this into the program that already defines func(double)):

public static void drawFunction(double xmin, double xmax, int width, int height) {
    if (width < 2 || height < 2) throw new IllegalArgumentException();
    double ymin = Double.POSITIVE_INFINITY, ymax = Double.NEGATIVE_INFINITY;
    int samples = Math.max(4, width * 2);
    for (int i = 0; i < samples; i++) {
        double x = xmin + i * (xmax - xmin) / (samples - 1);
        double y = func(x);
        if (y < ymin) ymin = y;
        if (y > ymax) ymax = y;
    }
    if (ymax == ymin) { ymax = ymin + 1; ymin = ymin - 1; }
    char[][] buf = new char[height][width];
    for (int r = 0; r < height; r++) java.util.Arrays.fill(buf[r], ' ');
    for (int col = 0; col < width; col++) {
        double x = xmin + col * (xmax - xmin) / (width - 1);
        double y = func(x);
        int row = (int) Math.round((ymax - y) / (ymax - ymin) * (height - 1));
        row = Math.max(0, Math.min(height - 1, row));
        buf[row][col] = '*';
    }
    if (0 >= ymin && 0 <= ymax) {
        int z = (int) Math.round((ymax - 0) / (ymax - ymin) * (height - 1));
        for (int c = 0; c < width; c++) if (buf[z][c] == ' ') buf[z][c] = '-';
    }
    if (xmin <= 0 && 0 <= xmax) {
        int zc = (int) Math.round((0 - xmin) / (xmax - xmin) * (width - 1));
        for (int r = 0; r < height; r++) {
            if (buf[r][zc] == ' ') buf[r][zc] = '|'; else buf[r][zc] = '+';
        }
    }
    for (int r = 0; r < height; r++) System.out.println(new String(buf[r]));
}

Notes and tips: sample more than one point per column to avoid gaps on steep curves; connect adjacent plotted rows if a continuous line is required; check whether func expects degrees or radians (common source of shape errors). This snippet provides a simple, testable base in line with 's suggestion to start small and iterate.

Also, to keep DaniWeb a student-friendly place to learn, don't expect quick solutions to your homework. We'll help you get started and exchange algorithm ideas, but only if you show that you're willing to put in effort as well.

Nobody here will do this for you. Make a start and come back here if you get stuck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.