+ 2

How can you draw lines in a circle knowing the angles?

I am working on a Windows Form in Visual C++, but is similar with C# and Visual Basic,so if you know in this ones i can try to adapt to what i want to do. Im looking in System:: Drawing:: Graphics library document and i figure it out how to draw a line and a circle.. Is there any function to input the angle for a second line?(based on the first one) Or should i make a function that know where to "put" the second line? Any ideas,tips,links are more then welcome :)

1st Nov 2017, 8:53 PM
derXred
derXred - avatar
22 odpowiedzi
+ 1
I hope I can help you, I used c# I draw a blue line and a red circle (which is called a ellipse where all sides are have equal length) on a winform. Graphics g = this.CreateGraphics(); //type Graphics is from system.drawing Pen pen1 = new System.Drawing.Pen(Color.Blue, 2F); g.DrawLine(pen1, 50, 50, 40, 20); Pen pen2 = new System.Drawing.Pen(Color.Red, 2F); g.DrawEllipse(pen2, 50, 50, 50, 50); http://dotnettutorials.com/tutorials/graphics/winforms-drawing-cs/ Next I looked for drawing lines with angles private void LineWithAngle(double angle) { double length = 350; double angleRadians = (Math.PI / 180.0) * angle; double x1 = 200; double y1 = 200; double x2 = x1 + (Math.Cos(angleRadians) * length); double y2 = y1 + (Math.Sin(angleRadians) * length); int intX1 = Convert.ToInt32(x1); int intY1 = Convert.ToInt32(y1); int intX2 = Convert.ToInt32(x2); int intY2 = Convert.ToInt32(y2); System.Drawing.Graphics graphicsObj; graphicsObj = this.CreateGraphics(); Pen myPen = new Pen(System.Drawing.Color.Green, 5); graphicsObj.DrawLine(myPen, new Point(intX1, intY1), new Point(intX2, intY2)); } And this i a little for loop with a nice result for (int i = 0; i < 360; i += 5) { LineWithAngle((double)i); } https://www.daniweb.com/programming/software-development/threads/420620/how-to-draw-a-line-having-length-and-angle
2nd Nov 2017, 8:45 PM
sneeze
sneeze - avatar
+ 2
OMG!!! is working!!! is alive!!! :)))) this is in C++ windows form based on .net framework(is not like the "pure" c++ beware) in case someone strugle to get it done: private: void LineWithAngle(double angle) { double length = 350; double angleRadians = (Math::PI / 180.0) * angle; double x1 = 200; double y1 = 200; double x2 = x1 + (Math::Cos(angleRadians) * length); double y2 = y1 + (Math::Sin(angleRadians) * length); int intX1 = Convert::ToInt32(x1); int intY1 = Convert::ToInt32(y1); int intX2 = Convert::ToInt32(x2); int intY2 = Convert::ToInt32(y2); System::Drawing::Graphics^ graphicsObj; graphicsObj = this->CreateGraphics(); Pen^ myPen = gcnew Pen(System::Drawing::Color::Green, 5); graphicsObj->System::Drawing::Graphics::DrawLine(myPen, *new Point(intX1, intY1), *new Point(intX2, intY2)); } I was missing the * for the new Point :) the rest i was getting it from DrawPie :D I was sure i did something else wrong :D I will come back with more questions if im stuck again :) First time when i search stuff in this area and it was overwhelming :) For beginners, great info after you understand it: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.drageventargs?view=netframework-4.7.1 THANK YOU again!!!
3rd Nov 2017, 5:43 AM
derXred
derXred - avatar
+ 2
@sneeze looks like is working if a i have a do-while loop{for loop } If i write a for loop{for loop} is getting only the first one Is this a thing? .... you cant use two for loop for drawing? :| I think i can make what i want with do-while and for loops, but i wonder why i cant use 2 for loop...is really odd or im missing something. Forgot to mention :) i have changed the center of the circle in the while-do loop(second loop), so using 2for loops is getting only one circle for some reason,but not because all the circles have the same center :)
3rd Nov 2017, 1:02 PM
derXred
derXred - avatar
+ 2
@sneeze i did that, but i dont understand why is working with a do-while loop and a for loop, and doesnt work with two for loops :) its odd. All the data is the same, right places for increment and stuff :)
3rd Nov 2017, 2:23 PM
derXred
derXred - avatar
+ 2
Hello sneeze, so im doing something wrong(but ive told i recheck it with a simple ouput just to verify de loop and was fine) or is some bug in the IDE or something... I got it done with different loop types :) but i dont know...i was just curious why i cant use 2for loops.. I almost finished my project(around 2000-2500 lines of code, is my biggest project that i ever made ...haha ) Not sure how could i add it here because of the multiple files... probably i cant...and is not something for daily-regular use...(some construction algorithm) Thank you again for the involvement and answers! :)
6th Nov 2017, 12:19 AM
derXred
derXred - avatar
+ 1
Im gonna read the code and try to adapt it :) i did something wrong with my code....i can draw something using a loop but from what i have been reading at some point wont draw what i want, and i got stuck in it because is something about where should i put the code. I did an object for the CreateGraphics() and tried to use values from an array(from a pure c++ file), i am not sure how to explain it :) i miss a lot probably. And thank you for the response :) i will try to make it work and come back with some code. I did something with DrawPie :) i could input the first line and the last one, is getting a bit rewrited but worked a little.
3rd Nov 2017, 5:00 AM
derXred
derXred - avatar
+ 1
@sneeze Works amazing :) Can i ask you something else?! If i want to make a second loop so i can make a second "form circle lines" .... how can i do it? I mean i have tried to make the second loop in the same function and the output is only the first cicle, the rest of them wont get on the screen...so i have tried to make 1 function with one loop and the used it in a second function with a second loop, same results. I am sure i am missing something.. I had the same problem with DrawPie, i was getting one pie,but i couldnt show the next one from a second loop that i before the first loop like you show it in the example. Forgot to tell you thank you for the links those are intersting and i didnt saw them before.
3rd Nov 2017, 12:48 PM
derXred
derXred - avatar
+ 1
@sneeze I hope i didnt mess smth now,i am doing a bit more to the code and i tried to "downgrade" it. private: void LineWithAngleC(double cent_angle, int x3, int y3) { double length = 100; double angleRadians = (Math::PI / 200.0) * cent_angle; double x1; double y1; x1 = x3; y1 = y3; // double x2 = x1 + (Math::Cos(angleRadians) * length); double y2 = y1 + (Math::Sin(angleRadians) * length); int intX1 = Convert::ToInt32(x1); int intY1 = Convert::ToInt32(y1); int intX2 = Convert::ToInt32(x2); int intY2 = Convert::ToInt32(y2); System::Drawing::Graphics^ graphicsObj; graphicsObj = this->CreateGraphics(); Pen^ myPen = gcnew Pen(System::Drawing::Color::Black, 0.1); graphicsObj->System::Drawing::Graphics::DrawLine(myPen, *new Point(intX1, intY1), *new Point(intX2, intY2)); //graphicsObj->System::Drawing::Graphics StringAlignment::10 //graphicsObj->Dispose(); Update(); } private: void Loop_draw(int spatiere1_x, int spatiere1_y) { double array_two[5][3]; for (int i = 0; i < 5; ++i) // { array_two[i][0] = asddsa::array_one[i][0]; array_two[i][1] = asddsa::array_one[i][1]; array_two[i][2] = asddsa::array_one[i][2]; LineWithAngleC((double)array_two[i][2], 50 + spatiere1_x, spatiere1_y); Update(); } Update(); } private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e) { //asddsa::array_one[5][3]; int i1 = 0; //when i use for loop i dont have this int spatiere_x = 100; int spatiere_y = 100; //for (int i1 = 0; i1 < 3; ++i1); do { Loop_draw(spatiere_x, spatiere_y); spatiere_x += 100; spatiere_y += 100; Update(); i1++; //when i use for loop i dont have this } while (i1 < 3);//when i use for loop i dont have this }
3rd Nov 2017, 3:49 PM
derXred
derXred - avatar
+ 1
@sneeze :) the semicolon was a mistake when i copy paste it, because i had smth else in that area and i tried to copy-paste it and adapt it to an earlier version of my code.....the debug was fine(no errors,just different outputs​), i have tried the code in a cpp before posting here :) so,still idk why i cant use 2 for loops...anyway, worked with do-while and one for loop. Now i get another "problem", if i use 2-3functions(all with one loop statment), the first one iterates once,move on and never come back to that...probably i will make another post with a clear example, maybe i dont know some condition for "jumping" from a function to another. In "pure" c++ i dont have this issues... Or maybe i will try it a bit more this night.. Thank you again for the help :)
3rd Nov 2017, 11:38 PM
derXred
derXred - avatar
+ 1
Hi derxred, I tried you code and the for-loop and while loop have the same result. At first I missed the third iteration but this was because my canvas was to small. When I enlarged it after the loop had run, the line is not redraw (which is actual correct but maybe less than you expected). So I made sure the canvas was big enough and than i saw 3 shapes. With some imagination it looked like the shape below. _\ /\ A not totally straight line, (maybe to 2 lines) from upper left to lower right. And 3 lines that meet the straight line in the same point. Maybe it are 5 lines meeting in the same point.
5th Nov 2017, 9:36 PM
sneeze
sneeze - avatar
+ 1
For debug have a look at OutputDebugStringW(L"My output string."); It depends on your project settings whicht version you need to use regarding unicode. https://stackoverflow.com/questions/1333527/how-do-i-print-to-the-debug-output-window-in-a-win32-app https://msdn.microsoft.com/en-us/library/aa363362(VS.85).aspx Use a listbox for the time you are testing or Write to a log file.
5th Nov 2017, 9:43 PM
sneeze
sneeze - avatar
0
My short answer is in all looping methods do the same. Almost everything you can do with a do-while loop you also can do with a for loop. You have to be aware of variables being in the right place, being incremented at the right moment. Do you use visual studio ? Can you debug / step through you code?
3rd Nov 2017, 2:09 PM
sneeze
sneeze - avatar
0
Can you post the code ?
3rd Nov 2017, 2:59 PM
sneeze
sneeze - avatar
0
thank you
3rd Nov 2017, 3:47 PM
sneeze
sneeze - avatar
0
my angles are in centesimal degrees...2π =400grades...1grade=100min and 1min=100sec...in case someone is wondering :) and a bit more for the code-comment(i cant get it in the same comment): In the click event if i use "for" i get one draw, if i use "do-while" i get 3 draws, thats why i wonder why is working with do-while and not with for, and i get no errors...really odd Next I want to delete the output if i click again the button and redraw the functio
3rd Nov 2017, 3:50 PM
derXred
derXred - avatar
0
Started off with making a Nested Loop example. Not sure if this is to easy for you, if so I am sorry. Also not sure if the problem is in the nested loop or in something else. https://code.sololearn.com/cWiroSn7LlY3 Gone have a look at your code now.
3rd Nov 2017, 8:17 PM
sneeze
sneeze - avatar
0
Why is there a semi-colon after your for-statement. Is it by accident or is it in your real code ? It ends the for-loop before it even can execute the body. for (int i1 = 0; i1 < 3; ++i1); At least in c# it does, not sure about c++
3rd Nov 2017, 8:45 PM
sneeze
sneeze - avatar
0
This is code I wrote in the playground to test your for and while loop. https://code.sololearn.com/c3b4UWGtlr74 The only thing I could find is the semi-colon so far.
3rd Nov 2017, 9:01 PM
sneeze
sneeze - avatar
0
Start logging either via watches and debug.messages or via a listbox on you winform Listbox1.items.add("log message"); If you log you see which path the code has followed. Can you define //asddsa::array_one[5][3]; you have commented this out but stil uses it in the methodes array_two[i][0] = asddsa::array_one[i][0]; Can you tell me which data should be there ?
4th Nov 2017, 1:16 PM
sneeze
sneeze - avatar
0
@sneeze I will try to understand the debug.meesages by inputing that in the code. I have this namespace in another file and i include it in the WindowsForm code: namespace asddsa { double array_one[5][3]= { {1, 2, 60}, {1, 3, 100}, {1, 4, 130}, {1, 5, 170}, {1, 6, 250} }; } sorry because i couldnt make an example(to be a clear example)...i had some personal issues... But ive tried to make the functions public...used some of the variables as global...stored them in global and local to try to get it right... And i follow them with some breakpoints...i have some odd values, and i will try to make it better and then i will try to ask again for some help
4th Nov 2017, 1:29 PM
derXred
derXred - avatar