ต้องลองปรับแต่งเพิ่มเงื่อนไขดูครับ
เช่น สุ่มเส้นขนานและไม่ขนานก่อน แล้วค่อยใส่เงื่อนไขต่อ
เส้นตัดขวาง ไม่ควรอยู่ริมขอบเกินไป กำหนด min max
ถ้าสุ่มได้เส้นไม่ขนาน กำหนด min max ของระยะห่างระหว่างเส้น
หรือ min max ของความเอียง(องศาของมุม)
ทำไมไม่ไปถาม AI ดู ถึงจะตอบแค่ basic แต่ก็ช่วยได้บ้าง
ตัวอย่างคำตอบที่ได้มา
There are a couple of issues with the code you provided that could be causing the strange output you're seeing:
1. The trigonometry functions Math.Cos(c) and Math.Sin(c) expect the angle to be in radians, not degrees. In the code, you're generating random angles in degrees and passing them directly to these functions, which will not produce the correct results. To convert degrees to radians, you can use the Math.PI constant and the following formula: radians = (degrees * Math.PI) / 180.
Code (C#)
float c = Ext.random.Next(1, 50);
c = (c * (float)Math.PI) / 180;
2. The line drawing is not correct, you should use a vector representation of the angle to draw the line, it's not correct to use a point representation. The correct way to calculate the line point is:
4. The variable Ext.random is not defined, it should be replace by Random() class.
Code (C#)
Random rand = new Random();
float c = rand.Next(1, 50);
5. You are drawing the lines and string on the graphics object of the picturebox but you are not saving the image, the next time you call the function the image will be draw on top of the previous one, you can use the DrawToBitmap method of the picturebox to save the image.
Code (C#)
var bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
pictureBox1.Image = bmp;
With these changes, the code should generate a more accurate representation of the lines and angles you're trying to draw.
Here is the complete code with all the changes I mentioned applied:
Code (C#)
private void button4_Click(object sender, EventArgs e)
{
Graphics dc = pictureBox1.CreateGraphics();
dc.Clear(Color.White);
Pen drawingPen = new Pen(Brushes.Black, 3);
PointF center = new PointF(pictureBox1.Width / 2F, pictureBox1.Height / 2F);
float radius = 200;
PointF rectOrigin = new PointF(center.X - radius, center.Y - radius);
RectangleF r = new RectangleF(rectOrigin, new SizeF(radius * 2F, radius * 2F));
dc.DrawEllipse(drawingPen, r);
Random rand = new Random();
float c = rand.Next(1, 50);
c = (c * (float)Math.PI) / 180;
PointF pointf = new PointF(center.X + radius * (float)Math.Cos(c), center.Y + radius * (float)Math.Sin(c));
dc.DrawLine(new Pen(Brushes.Black, 3), center, pointf);
dc.DrawString($"{(int)pointf.X},{(int)pointf.Y}", new Font("Arial", 11), new SolidBrush(Color.Black), pointf);
dc.DrawString($"\nมุม 1 = {c} องศา", new Font("Arial", 15), new SolidBrush(Color.Black), new Point(10, 150));
c += (rand.Next(30, 100) * (float)Math.PI) / 180;
pointf = new PointF(center.X + radius * (float)Math.Cos(c), center.Y + radius * (float)Math.Sin(c));
dc.DrawLine(new Pen(Brushes.Blue, 3), center, pointf);
dc.DrawString($"{(int)pointf.X},{(int)pointf.Y}", new Font("Arial", 11), new SolidBrush(Color.Black), pointf);
dc.DrawString($"\nมุม 2 = {c} องศา", new Font("Arial", 15), new SolidBrush(Color.Blue), new Point(10, 200));
c += (rand.Next(30, 100) * (float)Math.PI) / 180;
pointf = new PointF(center.X + radius * (float)Math.Cos(c), center.Y + radius * (float)Math.Sin(c));
dc.DrawLine(new Pen(Brushes.Red, 3), center, pointf);
dc.DrawString($"{(int)pointf.X},{(int)pointf.Y}", new Font("Arial", 11), new SolidBrush(Color.Black), pointf);
dc.DrawString($"\nมุม 3 = {c} องศา", new Font("Arial", 15), new SolidBrush(Color.Red), new Point(10, 250));
dc.DrawString($"{(int)center.X},{(int)center.Y}", new Font("Arial", 11), new SolidBrush(Color.Black), center);
var bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
pictureBox1.Image = bmp;
}
PointF A = new PointF(150, 50);
PointF B = new PointF(350, 50); // รูปล่าง 350, 70
PointF C = new PointF(100, 100);
PointF D = new PointF(300, 100); // รูปล่าง 300, 110
// เส้นตัดขวาง
PointF E = new PointF(150, 150);
PointF F = new PointF(300, 15);