Introduction to MATHFP use of fixed-point libraries in J2ME

Source: Internet
Author: User
Tags arithmetic clear screen integer mul thread

As we all know, CLDC1.0 does not support decimal operations, and CLDC1.1 only supports floating-point operations. But the current mobile phone, most of the use of CLDC1.0 this kind of configuration. What do we need to do with decimal operations? For example, to draw an arbitrary angle of the trajectory of the aircraft.

You can certainly write a class yourself, using integers to simulate fixed-point decimal operations (it is very difficult to simulate floating-point decimal operations), but you don't have to reinvent the wheel, there are many code libraries that use integer arithmetic to simulate decimal operations, and MATHFP is one of the very best, it's robust, stable, high-speed, is the best choice for decimal operations in a J2ME environment, and most crucially, it is small in size. MATHFP's download address is: Http://home.rochester.rr.com/ohom

Mes/mathfp.

The version I downloaded is based on CLDC, and the MATHFP version number of the download is 1.1.2. All the things you download back are a Mathfp.class (the package name for this class is Net.jscience.util), You can develop the class in your classpath, and add that class to the jar file when you publish the software.

Or you can decompile the class, get the source code, directly into your project SRC directory, I used here is the latter approach. Remember to download the MATHFP API documentation at the same time. Because MATHFP uses integers to simulate fixed-point decimals, so the inner representation of a decimal is still an integer, but you must remember to distinguish the integer representing the decimal number from the real integer, or else it will cause a lot of bugs that are difficult to debug (a small trick is to represent a decimal shape with the FP suffix). You only need to master a principle, is the first to participate in the decimal operation of the whole number into decimal, and then decimal operations, after the operation, and then convert the result into an integer use.

The following example is used to demonstrate the basic usage of MATHFP. Assume that there is a point in the lower left corner of the screen, and every 100 milliseconds, move 5 pixels along the 60-degree angle to the northeast, plotting the trajectory of this point. This example involves decimal and trigonometric operations because the increment of the x-coordinate of the point is COS60 (degree), and the increment on the y-coordinate is-sin60 (degree). Let's see how the code is written:

import Javax.microedition.lcdui.Canvas;


Import Javax.microedition.lcdui.Graphics;


import Net.jscience.util.MathFP;


/**


* Decimal Operation demo Canvas


* @author Jagie


*


*/


public class Floatcanvas


extends Canvas implements Runnable


{


//For Statistics screen refresh times


int paintcount;


//Screen width, height. Fixed-point number


int w_fp, H_FP;


//Current point coordinates, front point coordinates, fixed-point number


int curx_fp, CURY_FP,


LASTX_FP, LASTY_FP;


//Rate


public static final int RATE = 5;


public Floatcanvas ()


   {


w_fp = MATHFP.TOFP (This.getwidth ());


h_fp = MATHFP.TOFP (This.getheight ());


//Start point is in the lower left corner of the screen


lastx_fp = MATHFP.TOFP (0);


lasty_fp = H_FP;


New Thread (this). Start ();


   }


protected void Paint (Graphics g)


   {


//The first time just clear screen


if (paintcount = 0)


     {


g.setcolor (0);


g.fillrect (0, 0, W_FP, H_FP);


     }


Else


     {


//Draw line


G.setcolor (0X00FF00);


//Convert fixed-point number to integer


G.drawline (Mathfp.toint (LASTX_FP),


Mathfp.toint (LASTY_FP), MATHFP


. ToInt (CURX_FP), Mathfp.toint (CURY_FP));


     }


      


paintcount++;


    


   }


public void Run ()


   {


//Current point does not exceed screen time loop


while (curx_fp <= w_fp &&


cury_fp <= h_fp


&& mathfp.toint (CURX_FP) >= 0


&& mathfp.toint (CURY_FP)


>= 0) {


//60 degree angle conversion to radians


int radians =


Mathfp.div (Mathfp.mul (MATHFP.TOFP (60),


Mathfp.pi),


MATHFP.TOFP (180));


//x Direction Increment


int deltax = Mathfp.mul (MATHFP.TOFP (RATE),


Mathfp.cos (radians));


//y Direction Increment


int deltay = Mathfp.mul (MATHFP.TOFP (RATE),


Mathfp.sin (radians));


//new coordinates, fixed-point number


CURX_FP = lastx_fp + deltax;


cury_fp = Lasty_fp-deltay;


System.out.println (curx_fp + "," + cury_fp);


repaint ();


try {


Thread.Sleep (100);


catch (Interruptedexception e) {


//TODO auto-generated catch block


E.printstacktrace ();


       }


//new coordinates become old coordinates


lastx_fp = CURX_FP;


lasty_fp = CURY_FP;


     }


   }


}

The canvas is plotted on the device as shown below:

As you can see, the curve is growing in the direction of the northeast along the 60-degree angle. With this fixed-point library, we can use decimal arithmetic in the game, so some simple game physical algorithms can also be used.

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.