banner



How To Draw A Seiperneski Triangle In Java

The Android Canvas provides user-friendly methods for cartoon simple shapes such equally circles and rectangles, using drawCircle and drawRect respectively, merely beyond these the majority of shapes require some custom Path logic to draw. Two shapes that I was required to draw were triangles and rhombuses, so I thought I'd share how I achieved this.

As I mentioned above, the Paths can be used in conjunction with the drawPath method to describe any shapes or patterns you like. Usage is pretty straightforward, you merely accept to provide drawPath with a Path and Paint object and let it do the drawing:

Path path = ...; Paint paint = ...; canvas.drawPath(path, paint);        

But how practice you actually construct a path for a triangle or rhomb?

Triangles

Let's start with the triangle as information technology'south a slightly simpler shape than a rhomb. The mode a Path works is by cartoon lines betwixt points, so nosotros'll need to pick a point every bit the starting position, and draw lines to each vertex. As you'll see in the code below, nosotros'll be starting from the peak vertex and moving counter-clockwise around the triangle to each boosted vertex:

public void drawTriangle(Canvas canvas, Paint paint, int x, int y, int width) {        int halfWidth = width / 2;          Path path = new Path();     path.moveTo(10, y - halfWidth); // Top     path.lineTo(x - halfWidth, y + halfWidth); // Lesser left     path.lineTo(x + halfWidth, y + halfWidth); // Lesser correct     path.lineTo(x, y - halfWidth); // Back to Tiptop     path.close();          canvas.drawPath(path, pigment); }        

Simply call drawTriangle with the Canvass to draw on, the Paint to draw with, the X/Y coordinates to describe at, and the width of the triangle.

Let's define a Pigment object and see how it turns out:

Paint paint = new Paint(); paint.setColor(context.getColor(android.R.color.black));  drawTriangle(canvas, paint, 100, 100, 100);        

Drawing a Triangle on Android Canvas

Dandy, with a little customization such as modifying the color or applying antialiasing this can piece of work quite well for your triangle drawing needs.

Rhombus

Drawing a rhombus follows the same principle, just with an actress vertex. As you can see beneath, the Path is almost identical, just with the addition of a single actress vertex for the bottom of the rhomb:

public void drawRhombus(Canvas sail, Paint paint, int 10, int y, int width) {        int halfWidth = width / 2;          Path path = new Path();     path.moveTo(x, y + halfWidth); // Meridian     path.lineTo(x - halfWidth, y); // Left     path.lineTo(x, y - halfWidth); // Bottom     path.lineTo(x + halfWidth, y); // Right     path.lineTo(10, y + halfWidth); // Back to Top     path.close();          canvas.drawPath(path, paint); }        

Given the same Pigment as above, we can now draw the rhombus:

Paint pigment = new Pigment(); paint.setColor(context.getColor(android.R.color.black));  drawTriangle(sail, paint, 100, 100, 100); drawRhombus(canvas, paint, 100, 300, l);        

And we should meet a rhombus on the view like and then:

Drawing a Rhombus on Android Canvas

Other Shapes

Given you at present know how to draw triangles and rhombuses using custom paths, cartoon other shapes such as hexagons or octagons is simply a matter of adjusting the Path to form the shape you need. Additionally, the examples above assume a standard triangle and rhomb shape, merely adjusting to say, a right-bending triangle, can be accomplished by modifying the Path as well.

Source: https://kylewbanks.com/blog/drawing-triangles-rhombuses-and-other-shapes-on-android-canvas

Posted by: greenlyharsecy.blogspot.com

0 Response to "How To Draw A Seiperneski Triangle In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel