Krassnig / CodeDraw

CodeDraw is a beginner-friendly drawing library which can be used to create pictures, animations and even interactive applications.
https://krassnig.github.io/CodeDrawJavaDoc/
MIT License
18 stars 7 forks source link

drawTriangle() for integer values? #10

Closed rubyFeedback closed 1 year ago

rubyFeedback commented 1 year ago

Hey there Niklas and whoever else may read this,

I am currently working on the problem set in "Aufgabenblatt 2, Example 5". This is the free-form example where other students had solutions such as this:

https://i.imgur.com/h0AH6qX.png

So, for one of the shapes / subshapes that I want to draw I am contemplating using drawTriangle().

When I was using it, though, I noticed I had to use double values (6 of them, e. g. for the 3 vertices or end points or however one calls them).

API is:

public void drawTriangle(double x1,  double y1,  double x2,  double y2,  double x3,  double y3)

x1 - The distance in pixel from the left side of the canvas to the first corner of the triangle.
y1 - The distance in pixel from the top side of the canvas to the first corner of the triangle.
x2 - The distance in pixel from the left side of the canvas to the second corner of the triangle.
y2 - The distance in pixel from the top side of the canvas to the second corner of the triangle.
x3 - The distance in pixel from the left side of the canvas to the third corner of the triangle.
y3 - The distance in pixel from the top side of the canvas to the third corner of the triangle.

My canvas, though, right now only needs integer values. Or at the least I was thinking I was using integer values for x-coordinate and y-coordinate of the various subshapes.

It is not a big deal either way; I can convert the integer values into double values. But I was also thinking that there should not be a reason to not allow integer values here? Or perhaps there is one and I am unaware of that - that could be the case too.

Either way, the TL;DR variant for this suggestion:

Would it be possible to add the "same" method signature as-is, but also allow integer values for x1, y1 etc...?

The primary rationale here is just for a bit of extra convenience. It's not necessary since we can convert our integer to double values easily, but if CodeDraw directly could support this then we can save a tiny bit of code in our implementation.

As always thank you for reading either way.

NikKasyan commented 1 year ago

Hey @rubyFeedback. First of all, thank you very much for your interest in CodeDraw and your suggestion.

You shouldn't really need to convert the integers by hand, Java should be able to this task automatically. For example if you want to draw a triangle which spans the whole CodeDraw window you can use either of these codes (remember getWidth and getHeight both return integers).

cd.drawTriangle(cd.getWidth()/2, 0, 0, cd.getHeight(), cd.getWidth(), cd.getHeight());

or

cd.drawTriangle((double) cd.getWidth() / 2, (double) 0, (double) 0, (double) cd.getHeight(), (double) cd.getWidth(), (double) cd.getHeight());

Actually, in the second example Intellij will highlight the double cast gray because the conversion is not necessary.

If you find an example in which drawTriangle doesn't accept int please provide an example, otherwise please close the issue. Hopefully I could resolve your issue and thank you for your active participation.