Skip to content

Instantly share code, notes, and snippets.

@ZirconCode
Created October 16, 2014 08:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZirconCode/59cae1e2615279eafb29 to your computer and use it in GitHub Desktop.
Save ZirconCode/59cae1e2615279eafb29 to your computer and use it in GitHub Desktop.
public void drawTriangle(Point p1, Point p2, Point p3, int clr, Canvas c)
{
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStrokeWidth(1);
paint.setColor(clr);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setAntiAlias(true);
Path path = new Path();
path.setFillType(Path.FillType.EVEN_ODD);
path.moveTo(p1.x,p1.y);
path.lineTo(p2.x,p2.y);
path.lineTo(p3.x,p3.y);
path.close();
c.drawPath(path, paint);
}
@MakarchukR
Copy link

Update version.
private static void drawTriangle(int x, int y, int width, int height, boolean right, boolean high, Paint paint, Canvas canvas) {

    Point p1;
    Point p2;
    Point p3;

    if (right) {
        if(high) {
            p1 = new Point(x, y);
            p2 = new Point(x + width, y);
            p3 = new Point(x, y + height);
        } else {
            p1 = new Point(x+width, y+height);
            p2 = new Point(x +width, y);
            p3 = new Point(x, y + height);
        }
    } else {
        if(high) {
            p1 = new Point(x, y );
            p2 = new Point(x+width, y+height);
            p3 = new Point(x+width, y);
        } else {
            p1 = new Point(x, y );
            p2 = new Point(x+width, y+height);
            p3 = new Point(x, y+height);
        }
    }


    Path path = new Path();
    path.setFillType(Path.FillType.EVEN_ODD);
    path.moveTo(p1.x, p1.y);
    path.lineTo(p2.x, p2.y);
    path.lineTo(p3.x, p3.y);
    //path.lineTo(p1.x, p1.y);
    path.close();

    canvas.drawPath(path, paint);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment