اذهب إلى المحتوى
  • 0

كيف أرسم مستطيل مع قطريه على #console C؟

Simoh

السؤال

أريد رسم مستطيل على console بلغة #C، وحقيقة أتضح لي أن الأمر صعب بعض الشيء لأنه يلزمني حسابات وبعض الأمور الرياضية لفعل ذلك، فهل أجد لديكم من توضيح حول الأمر؟ أو أي مساعدة على شكل كود برمجي؟

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

إليك هذا المثال الذي استعنت به في أحد التطبيقات، هو بلغة #C وسينفعك :

using System;

public class Test
{
    static int width;
    static int height;

    public static void Main()
    {
        Console.Write("Enter width of rectangle: ");
        width = int.Parse(Console.ReadLine()); 
        Console.Write("Enter height of rectangle: ");
        height = int.Parse(Console.ReadLine()); 
        Console.WriteLine();
        if(width > 5 && width <= 80 && height > 5 && height <= 20)
        {
            draw();
        }
        else
        {
            Console.WriteLine("Invalid entry!");
        }
    }

    static void draw()
    {
        Console.WriteLine((float)width/(float)height);
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                if (IsBorder(x, y)  || IsDiagonal(x, y))
                {
                    Console.Write("*");
                }
                else
                {
                    Console.Write(" ");
               }

            } Console.WriteLine();
        }
    }

    static bool IsBorder(int x, int y)
    {
        return x == 0 
            || y == 0  
            || x == width - 1  
            || y == height - 1;
    }

    static bool IsDiagonal(int x, int y)
    {
        return width < height  
            ? IsDiagonalHigh(x, y)  
            : IsDiagonalWide(x,y);
    }

    static bool IsDiagonalHigh(int x, int y)
    {
        var aspectRatio = (float)width / (float)height;
        return x == (int)(y * aspectRatio) 
            || x == width - (int)(y * aspectRatio) - 1;
    }

    static bool IsDiagonalWide(int x, int y)
    {
        var aspectRatio = (float)height / (float)width;
        return y == (int)(x * aspectRatio)  
            || y == (int)((width - x - 1) * aspectRatio);
    }

}
رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...