Triangle fill in MIDP1.0

xiaoxiao2021-03-06  54

Fill in the triangular area in MIDP1.0. Due to the development of the adaptability, J2ME programmers have not used MIDP 2.0 for many times. But the functionality of MIDP1.0 is much different from 2.0, and many practical functions are not available. This requires programmers to achieve themselves. This article describes the practical functions in Canvas to populate the triangle area in Canvas.

Import javax.microedition.lcdui.canvas; import javax.microedition.lcdui.graphics;

/ * * Created on 2005-3-30 * * to change the Template for this generated file go to * window - preferences - java - code style - code templates * /

/ ** * @author cuilichen * * To change the template for this generated type comment go to Window - * Preferences - Java - Code Style - Code Templates * / public class Triangle extends Canvas {private Graphics g; public Triangle () {}

Protected void Paint (graphics arg0) {g = arg0; g.setcolor (0xffff); g.fillRect (0, 0, this.getwidth (), this.getHeight ()); g.setColor (0x000000); Point Point0 = New Point (10, 10); Point Point1 = New Point (100, 30); Point Point2 = New Point (200, 160); Long Time1 = System.currentTimeMillis (); // for (INT i = 0; i < 100; i ) FillTriangle (Point0, Point1, Point2); long time2 = system.currenttimemillis (); system.out.println ("Time IS" (Time2 - Time1));}

/ ** * Condition: Point POINT0 (X0, Y0), POINT1 (X1, Y1), POINT2 (X2, Y2) constitute a triangle. Workaround: Setting straight L over Point1, POINT2 two points * (x3, y3) are points on the line L, from (X0, Y0) to (X3, Y3). As long as we take all the displayed pixel points above the straight line L, you can implement a triangular fill. * * Problem: There are three states, vertical, horizontal, and tilt, vertical, horizontal, and we are easy to solve. * Tilt state, we need to find the slope of the line, which seems to use floating point numbers, but we can use an integer to solve. * Because when the two endpoints of POINT1, POINT2 on the line (line segment), the horizontal coordinate X1! = X2 (the absolute value of X1-X2 is greater than or equal to 1), the difference in the longitudinal coordinate Y2-Y1 is the high screen. . Therefore, (Y1-Y2) / (x1-x2) value does not exceed the height value of the screen. * This value is much different from the Integer.max_Value. This way we can use this slope to zoom in 1000 times. This is a requirement for the level of slope.

* / Private Void FillTriangle (Point Point0, Point Point1, Point Point2) {INT X3, Y3, State; State = DealwithPoints (Point0, Point1, Point2); if (State == line.upright) {// vertical x3 = Point1.x; y3 = POINT1.Y 1; do {g.drawline (Point0.x, Point0.y, x3, y3); Y3 ;} while (Y3 Point2.x) {// guarantees Point1 on the left Exchange (Point1, Point2);} Y3 = Point1.y 1; x3 = (Y3 - Point1.y) * 1000 / k Point1.x; do {// horizontal draws over G. Drawline (Point0.x, Point0.y, x3, y3) ; X3 ; y3 = (x3 - point1.x) * k / 1000 point1.y;} while (x3

/ ** * Treat three points, which point does Point0, which point do Point1, which point do Point2. * Because Point0 will be the initial point of the "ray", the other two points are named POINT1, and POINT2 is simultaneously adjusted.

* The meaning of this handling is the convenience of making the drawing processing * / private INT DEALWITHPOINTS (Point Point0, Point Point1, Point Point2) {if (Point 0.x == Point1.x) {/ {// Determine the opposite of the ray State, horizontal, vertical or tilt, and adjust Exchange (Point0, Point2); if (Point1.y> Point2.y) {// guarantees Point1 Exchange (Point1, Point2) } Return line.upright;} else if (point0.x == point2.x) {Exchange (Point0, Point1); if (Point1.y> Point2.y) {// guarantees that Point1 is on the upper side Exchange (Point1, Point2);} return line.upright;} else if (point2.x == point1.x) {if (point1.y> point2.y) {// guaranteed Point1 Exchange (Point1, Point2);} Return Line {Exchange (Point0, Point2); if (Point1.x> Point2.x) {// guarantees that Point1 is on the left Exchange (Point1, Point2);} return Line.Plane;} else if (Point0.Y) == POINT2.Y) {Exchange (Point0, Point1); if (Point1.x> Point2.x) {// guarantees that Point1 is on the left Exchange (Point1, Point2);} Return Line.Plane;} else if (Point2. y == point1.y) {if (Point1.x> Point2.x) {// guaranteed Point1 on the left Exchange (Point1, Point2);} return line.plane;} else {if (Point1.y> Point2.y ) {/ Guaranteed Point1 Exchange (Point1, Point2);}}}

Private Void Exchange (Point A, Point B) {// Exchange two points of Reference Int Tmp; TMP = AX; AX = Bx; BX = TMP; TMP = AY; AY = by; BY = TMP;} Class line { / / Define three states, horizontal, vertical, tilted public static final int shape = 0; // level

Public static final int UPRIGHT = 1; // vertical

Public static final int inCline = 2; // tilt}

Class Point {// definition point, including cross-sectional PRIVATE INT X, Y;

PUBLIC POINT () {this (0, 0);

Public Point (int x, int y) {this.x = x; this.y = y;}}

}

The time test and stability test of the code are passed. But there are still some defects, such as whether there is no three points to form a triangular detection.

When the triangular area is large, there is more time to run, but no more than 0.06 seconds, can endure. In addition, if the fill is not very complete, you can adjust the first position of the three points, you can. For example, FillTriangle (Point1, Point0, Point2);

If you feel that you don't insurance, you can "fill it directly twice":

FillTriangle (Point0, Point1, Point2);

FillTriangle (Point1, Point0, Point2);

Well, just like this, I can contact me if I have a problem.

MSN: Cuilichen@hotmail.com

转载请注明原文地址:https://www.9cbs.com/read-40854.html

New Post(0)