Python challenge | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Python challenge

I want to draw a fill triangle with python's tkinter just with using create_line (1px between every line to fill that triange) any idea?????

19th Dec 2019, 8:14 AM
Roxana
33 Answers
+ 4
Mσみคოคの ʝคงค๔ คłıķみσňı I think it is already explained, but here is a shorter one: The way I did was that I used horizontal lines only. With x1 and x2 being the limit how far the line stretches. To get x1 and x2, we need to solve the linear equations of the lines. After that, let say the top of the triangle has coordinates (10, 80) and the bottom a coordinates (40, 120). You can clearly see that the horizontal lines should be between 80 and 120. We loop throught these two values, and each time replace y in the line equations to get x1 and x2. It's just basic math, but implementing them in programming was a bit tricky.
19th Dec 2019, 12:26 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
This question has been asked two days ago, but no one gave an answer yet. Can you send a link or image to the original challenge ?
19th Dec 2019, 8:32 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
Where did you find it ? I did try to solve it with math, using trigonometric formulas, but it seemed complicated and more related to math than programming, so I just wanted to see the challenge to know if there was some extra info that both of you could have missed
19th Dec 2019, 8:43 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
Sounds like you would draw diagonal lines pixel by pixel which get shorter the farther right you go. Conceptually like this. * * + * + - * + - < * + - < #
19th Dec 2019, 8:59 AM
HonFu
HonFu - avatar
+ 1
Okay, that's probably easier. 😅
19th Dec 2019, 9:05 AM
HonFu
HonFu - avatar
+ 1
HonFu yes, that how I thought of it too, but finding the coordinates of the horizontal lines is not as obvious as it seems
19th Dec 2019, 9:05 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
HonFu no, it is the same idea as yours, it's jst that you have to take in consideration different types of triangles, and different angles
19th Dec 2019, 9:06 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
4. (Continuation) and at the same time you loop throught the X1_Coordinates and X2_Coordinates, and you create multiples lines using this coordinates I know this can be confusing, I'll try to write a working code as soon as I can
19th Dec 2019, 9:19 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
Roxana that would only work if the bottom line was horizontal, otherwise you'll get completly messed up shape
19th Dec 2019, 9:20 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
Roxana I think I have solved it, but I didn't give it much tests, and the code is badly written. Do you just want the solution or you still gonna try ?
19th Dec 2019, 11:42 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
Mσみคოคの ʝคงค๔ คłıķみσňı actually I would prefer it much better to see you try and solve it. I don't usually give codes just like that, but since this, in my opinion, is more about maths that programming, I think I'll make an exception. If you have any problems with the code, you can ask here so I can clarify, even thought I already explained everything in the text above. Here is the code: https://code.sololearn.com/cj8mx4uj7nFY/?ref=app This code was for testing purposes, so I didn't write it very well, if you still want a better version of it, ask again. I'll write it once I have free time.
19th Dec 2019, 11:48 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
Sorry, I don't know any links or image about this
19th Dec 2019, 8:38 AM
Roxana
0
Just a challenge between friends, but i can't find a fixed algorithm to find all points and create line between them, that can fill any triangle...
19th Dec 2019, 8:53 AM
Roxana
0
Yes but i need to find a algorithm that works for any triangle
19th Dec 2019, 9:02 AM
Roxana
0
The idea is: 1. You find the linear equation of all three sides of the triangle 2. Determine the highest, middle, and lowest point (you just sort them with the y coordinate) 3. This is the tricky part: The idea of this part is to draw horizontal lines, starting from the top point, here is how it goes: a) One line will contains all the x coordinates, it is the line containing the top and bottom points, finding them is easy, you just replace the y coordinate in the equation you found in step 1, let name this list X1_Coordinates (try visualizing it on a paper) b) You do the same thing for the two remaining lines, but you must repesct the order of who is on top of the other c) with all this done, comes the easiest part, all you have to do now is concatenate the two lists you got in the final steps, there you got your X2_Coordinates list 4. Finally, you loop throught the y coordinates in range(Yi, Yj) with Yi and Yj being the y coordinate of the highest and lowest points respectivly (continued)
19th Dec 2019, 9:04 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
Aymane Boukrouh [Unavailable] I think i get it my own idea was line equation too but it didn't work on my way; i just make a 6 list for all of that 3 line's points and try to create line between them but it doesn't work well.
19th Dec 2019, 9:17 AM
Roxana
0
Aymane Boukrouh [Unavailable] Thank you so much, I'll try it.
19th Dec 2019, 9:26 AM
Roxana
0
Aymane Boukrouh [Unavailable] Can we see the code?thank you
19th Dec 2019, 11:45 AM
Mσみคოคの ʝคงค๔ คłıķみσňı
Mσみคოคの ʝคงค๔ คłıķみσňı - avatar
0
Aymane Boukrouh [Unavailable] Thank you for taking the time.I hope we get the result ⁦❤️⁩💛
19th Dec 2019, 11:58 AM
Mσみคოคの ʝคงค๔ คłıķみσňı
Mσみคოคの ʝคงค๔ คłıķみσňı - avatar
0
Mσみคოคの ʝคงค๔ คłıķみσňı no problem, as I said I haven't tried many examples, but so far all the 10 test were successful on my laptop. If a test case gives a messy shape, give me the coordinates you tried it with so I can debug my code
19th Dec 2019, 12:00 PM
Aymane Boukrouh
Aymane Boukrouh - avatar