Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > C# winApp รบกวนช่วยหน่อยครับ DrawString Drawline Drawarc



 

C# winApp รบกวนช่วยหน่อยครับ DrawString Drawline Drawarc

 



Topic : 136926



โพสกระทู้ ( 4,434 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook



ตัว KidsLearning เริ่มเดินทางมาถึงในส่วนของ เส้นตรง มุม แล้วก็การวาดรูปทรงต่างๆ แล้ว ครับ
จากเนื้อหาที่จะทำ จะเป็นการวาดเส้น หรือ วงกลม จากนั้น ก็จะมีการระบุมุม หรือ ข้อความลงไปแบบนี้ ครบั

22

1311

Cr.
https://www.dltv.ac.th/teachplan/episode/22369
หลักสูตรในโปรแกรมผมใช้ dltv แทบทุกเนื้อหาครับ เพียงแค่อาจจะเอามารวมๆ ให้เป็น ชุดเดียวกัน เพราะความสะดวก

จุดที่ผมคิดไม่ออกเลยคือ
1. พอดีสุ่มพิกัด และ ลากเส้น 2 เส้น ไปตัดกันแล้ว จะรู้ได้ยังไงว่าไปตัดกันที่ตำแหน่ง x,y ใด
2. จุดพิกัดที่จะ Drawarc จะเริ่มจากจุดไหน ไปยังจุดไหน

ตอนนี้ยังทำไม่ได้ ก็เลย ข้ามไปทำเรื่อง สถิติ ไปพลางๆ ก่อนครับ



Tag : .NET, Win (Windows App), C#







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2023-01-17 10:53:12 By : lamaka.tor View : 394 Reply : 8
 

 
1. https://stackoverflow.com/a/565282
2. ตำนวณระยะจากเส้นขนานและเส้นตัดขวาง ให้มีระยะห่างเท่ากันตามความเหมาะสม ขึ้นอยู่กับค่าที่ random ของเส้นทั้งสองประเภท และต้องหาจุดตัดตามข้อ 1 ให้ได้ก่อน เพราะต้องเริ่มวัดระยะจากจุดนี้

Quote:
โจทย์ลักษณะนี้ไม่เหมาะกับการ random
วาดเองแล้วเก็บเป็นรูปาพ เวลาใช้ สุ่มเฉพาะชื่อรูป จะง่ายกว่า







แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-01-17 12:53:58 By : 009
 


 

No. 2



โพสกระทู้ ( 4,434 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

ตอบความคิดเห็นที่ : 1 เขียนโดย : 009 เมื่อวันที่ 2023-01-17 12:53:58
รายละเอียดของการตอบ ::
ขอบคุณ ครับ
ผมเพิ่งอ่าน และ ศึกษาเนื้อหา ยังไม่ได้ลองถึงความเป็นไปได้ ครับ
พอดีลากบนกระดาษ หรือ ลากใน word มันก็ง่ายดีอยู่
แต่พอเอาเข้ามาใช้ DrawString Drawline Drawarc ก็กลายเป็นว่า ยังติดที่จุดพิกัด ต่างๆ อยู่


เดี๋ยวต้องศึกษาเยอะๆ กว่านี้หน่อยครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-01-17 14:35:36 By : lamaka.tor
 

 

No. 3



โพสกระทู้ ( 4,434 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

ดูๆ เหมือนจะไม่รอด
ผมลองเขียนโค้ดง่ายๆ แค่ให้มีเส้นตรง 3 เส้น แล้วพยายามให้ตัดกัน ประมาณนี้


Code (C#)
Random random = new Random();
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
         Pen redPen1 = new Pen(Color.Red, 5);
         redPen1.StartCap = LineCap.ArrowAnchor;
         redPen1.EndCap = LineCap.ArrowAnchor;

         e.Graphics.DrawLine(redPen1, random.Next(50,100), random.Next(50, 100), random.Next(400, 700), random.Next(50, 100));

         Pen redPen2 = new Pen(Color.Black, 5);
         redPen2.StartCap = LineCap.ArrowAnchor;
         redPen2.EndCap = LineCap.ArrowAnchor;
         e.Graphics.DrawLine(redPen2, random.Next(50, 600), random.Next(5, 20), random.Next(50, 600), random.Next(100, 300));

         Pen redPen3 = new Pen(Color.Red, 5);
         redPen3.StartCap = LineCap.ArrowAnchor;
         redPen3.EndCap = LineCap.ArrowAnchor;

         e.Graphics.DrawLine(redPen3, random.Next(50, 100), random.Next(50, 100), random.Next(400, 700), random.Next(50, 100));
        }


ลองรันมันได้มาประมาณนี้ ครับ

1

2

3

นี่คือ ผลที่รัน 3 รอบต่อๆ กัน ครับ ไม่มีชิ้นดี ซักรูป ครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-01-17 15:11:27 By : lamaka.tor
 


 

ต้องลองปรับแต่งเพิ่มเงื่อนไขดูครับ
เช่น สุ่มเส้นขนานและไม่ขนานก่อน แล้วค่อยใส่เงื่อนไขต่อ
เส้นตัดขวาง ไม่ควรอยู่ริมขอบเกินไป กำหนด min max
ถ้าสุ่มได้เส้นไม่ขนาน กำหนด min max ของระยะห่างระหว่างเส้น
หรือ min max ของความเอียง(องศาของมุม)

นี่ยังไม่นับการหาจุดตัดและใส่ความกว้างของมุม
ลองทำไปก่อน อาจเป็นผู้บุกเบิกก็ได้
ไม่ได้จริง ค่อยสุ่มเป็นรูปภาพตามที่ผมบอก



จริงๆ ลองถาม AI อาจได้ไอเดียดีๆ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-01-17 15:33:24 By : 009
 


 

No. 5



โพสกระทู้ ( 4,434 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

ตอบความคิดเห็นที่ : 4 เขียนโดย : 009 เมื่อวันที่ 2023-01-17 15:33:24
รายละเอียดของการตอบ ::
รู้สึกว่า ทำไมมันยากกว่าไปเรียนละครับเลย 555
รู้งี้ ขยันเรียนตั้งแต่เด็กก็ดี

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-01-17 15:46:01 By : lamaka.tor
 


 

No. 6



โพสกระทู้ ( 4,434 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

ไม่ทราบว่าผมเขียนโค้ด หรือ อัลกอ ผิดตรงไหน มุมมันออกมาแปลกๆ ครับ
34

36

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);
            
            float c = Ext.random.Next(1, 50);
            
            string s="";//= "มุม 1 = " + c;
            PointF pointf = new PointF((int)(center.X + radius * Math.Cos(c)), (int)(center.Y + radius * 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 += Ext.random.Next(30, 100);

            pointf = new PointF((int)(center.X + radius * Math.Cos(c)), (int)(center.Y + radius * 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 += Ext.random.Next(30, 100);
            pointf = new PointF((int)(center.X + radius * Math.Cos(c)), (int)(center.Y + radius * 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);
        }



อ้างอิง
http://www.ucancode.net/Visual_C_MFC_Samples/CSharp_Example_Free_DOTNET_Gauge_Control_Draw_Source_Code.htm

55
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-01-17 22:58:54 By : lamaka.tor
 


 
ตอบความคิดเห็นที่ : 6 เขียนโดย : lamaka.tor เมื่อวันที่ 2023-01-17 22:58:54
รายละเอียดของการตอบ ::

Quote:
ทำไมไม่ไปถาม 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:

Code (C#)
    pointf = new PointF(center.X + radius * (float)Math.Cos(c), center.Y + radius * (float)Math.Sin(c));


3. The variable s is not used, you can remove it

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;
}

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-01-21 10:33:36 By : 009
 


 
โดนจูงเข้าป่าไปหลายรอบ ได้ใกล้เคียงสุดประมาณนี้

e

ne


จุดของเส้นทั้งสามตามรูปบน

Code (C#)
            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);


ที่เหลือลองถาม AI เรื่องการหาจุดตัด และองศาของมุม
งานยากน่าจะอยู่ที่ทำให้มัน generate ได้
เพราะต้องใช้เวลาปรับค่าหา min/max ของแต่ละจุด
เพื่อให้รูปออกมาดูดี พอไปวัดได้




วิธีถาม AI พยายามแยกถามทีละส่วน ถ้าถามรอบเดียวแบบจบงานเลย...เป๋
(ไม่งั้น สู้ศึกษาตำรา Math แล้วแทนค่าเอง + พึ่งพวก stackฯ ไม่ได้ )
อีกอย่างบางช่วงมี request เข้าไปเยอะ มันจะตอบไม่หมด
และถ้าเป็นคำถามเดียวกัน แต่ถามซ้ำ จะได้คำตอบในแต่ละครั้งไม่เหมือนกัน


Quote:
ทุกสรรพสิ่งมีดีย่อมมีเสีย

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-01-24 22:32:59 By : 009
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : C# winApp รบกวนช่วยหน่อยครับ DrawString Drawline Drawarc
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่