Java draw a Curve instance code (two JSP drawing curve instance) (1/2)

Source: Internet
Author: User
Tags cos sin

Java draw a Curve instance code (two pages special Effects phttp://www.111cn.net/Web page special effects p.html target=_blank >jsp Tutorial Draw curve instance)
/*
Description of parameter names
To draw a pattern required; The parameter value can be the following constants: 6912, #点, the drawn polygon is displayed as a point, 6913, #线, the drawn polygon is displayed as a segment;
Required for the beginning of a curve, integral type. The value of the parameter should be between 0 and the "U to equal fraction" in the "Set curve points" command.
Required for the end of a curve, integral type. The value of the parameter should be between 0 and "point to equal points" in the "Set Curve" command.
*/

public class Test {
public static void Main (string[] args) {
Trifunc tri = new Trifunc ();

Generate a 25x100 canvas
Canvas canvas = new Canvas (25, 120);

Draw the sin curve, with a period of 2
Tri.drawsin (canvas, 2.0);
Canvas.printcanvas ();

System.out.println ();
Canvas.reset ();
Draw the Cos curve, with a period of 2
Tri.drawcos (canvas, 2.0);
Canvas.printcanvas ();
}
}

Class Trifunc {

/**
* Draw Sin Curve
* @param canvas Canvas
* @param period Curve cycle
*/
public void Drawsin (canvas canvas, double period) {
char[][] chars = Canvas.getcanvas ();
Ratio of x axis
Double Xratio = (2 * period * Math.PI)/(Canvas.getwidth ()-1);
Magnification of y-axis
int ymulti = (Canvas.getheight ()-1)/2;
for (int i = 0; i < canvas.getwidth (); i++) {
Mapping an array index to a horizontal axis value
Double k = (I-canvas.getwidth ()/2) * xratio;
To map a sin value to an array index
int h = ymulti-(int) Math.Round (Math.sin (k) * ymulti);
Chars[h][i] = Canvas.fill_char;
}
}

/**
* Draw Cos Curve
* @param canvas Canvas
* @param period Curve cycle
*/
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;
}
}
}

Home 1 2 last page
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.