Help with a task

Tatycoco

New member
Joined
Jan 10, 2025
Messages
4
Programming Experience
Beginner
Hello, just started learning C# at my univeristy, im sorry if this aint the best place to post this kind of things. I need help with a task . I missed one lesson and i can't do this. I've been racking my brain so hard, and i can't figure it out.
Thank you!
 

Attachments

  • image_2025-01-10_172608192.png
    image_2025-01-10_172608192.png
    329.4 KB · Views: 4
We won't do your homework for you.

What have you thought about? What have your considered?

Show us an outline of your plan to tackle the problem. We can guide you along the way by asking some probing questions or giving suggestions.
 
Ok, I understand. So i've managed to draw random ornaments and spread them if the width of the window changes, but i can't manage to autoscale them if the height of the window changes.
 

Attachments

  • image_2025-01-10_175601672.png
    image_2025-01-10_175601672.png
    17.1 KB · Views: 3
You need to show us what you've done, or a simplified example of what you've done that demonstrates the relevant behaviour, and explain exactly where the issue is in that code. We can't tell you how to modify code we haven't seen.

That said, if you expect your drawing to be proportional to the height of the form then you will need to start with the height of the form and then divide that by some appropriate value to get the height of the drawing. The same goes for the width.
 
I'm sorry if I'm being too imprecise, but im new to this kind of things.

C#:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace C__Project_3
{
    public partial class Form1 : Form
    {
        private class Ornament
        {
            public int X { get; set; }
            public int Type { get; set; }
        }

        private List<Ornament> ornaments = new List<Ornament>();
        private int startY = 50;
        private int spacing = 50;

        public Form1()
        {
            InitializeComponent();
            this.Resize += WindowResized;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitializeOrnaments();
        }

        private void WindowResized(object sender, EventArgs e)
        {
            AddNewOrnaments();
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics g = this.CreateGraphics();
            Pen bluePen = new Pen(Color.Blue, 3);
            Pen redPen = new Pen(Color.Red, 3);
            Pen greenPen = new Pen(Color.Green, 3);
            Pen pinkPen = new Pen(Color.HotPink, 3);
            Pen turquoisePen = new Pen(Color.Turquoise, 3);
            Pen purplePen = new Pen(Color.Purple, 3);
            Pen goldPen = new Pen(Color.Gold, 3);

            g.DrawLine(bluePen, 0, startY, this.ClientSize.Width, startY);

            for (int i = 0; i < ornaments.Count; i++)
            {
                int startX = ornaments[i].X;
                g.DrawLine(bluePen, startX, startY, startX, startY + 30);

                switch (ornaments[i].Type)
                {
                    case 1:
                        g.DrawRectangle(pinkPen, startX - 20, startY + 30, 40, 40);
                        g.DrawEllipse(turquoisePen, startX - 20, startY + 30, 40, 40);
                        break;
                    case 2:
                        Point[] diamondPoints = new Point[4]
                        {
                            new Point(startX, startY + 30),
                            new Point(startX - 20, startY + 50),
                            new Point(startX, startY + 70),
                            new Point(startX + 20, startY + 50)
                        };
                        g.DrawPolygon(redPen, diamondPoints);
                        g.DrawEllipse(greenPen, startX - 20, startY + 30, 40, 40);
                        break;
                    case 3:
                        Point[] trianglePoints = new Point[3]
                        {
                            new Point(startX, startY + 30),
                            new Point(startX - 20, startY + 50),
                            new Point(startX + 20, startY + 50)
                        };
                        g.DrawPolygon(purplePen, trianglePoints);
                        break;
                    case 4:
                        g.DrawRectangle(goldPen, startX - 20, startY + 30, 40, 40);
                        Point[] invertedTrianglePoints = new Point[3]
                        {
                            new Point(startX, startY + 30),
                            new Point(startX - 20, startY + 70),
                            new Point(startX + 20, startY + 70)
                        };
                        g.DrawPolygon(redPen, invertedTrianglePoints);
                        break;
                }
            }
        }

        private void InitializeOrnaments()
        {
            Random random = new Random();
            for (int startX = spacing; startX < this.ClientSize.Width; startX += spacing)
            {
                int type = random.Next(1, 5);
                ornaments.Add(new Ornament { X = startX, Type = type });
            }
        }

        private void AddNewOrnaments()
        {
            Random random = new Random();
            int startX = Math.Max(spacing, ornaments.Count * spacing);

            for (int x = startX; x < this.ClientSize.Width; x += spacing)
            {
                int type = random.Next(1, 5);
                ornaments.Add(new Ornament { X = x, Type = type });
            }
        }
    }
}
 
Also, make sure that you dispose all your Pen objects, and anything else that supports it. You should create them with a using statement, so they are disposed implicitly when you're done.
 
And for scaling, see this API:

Unless your teacher told your class specifically not to use that API. In which case, you'll need to scale all your magic numbers in your code based on the ratio of the current rectangular height vs your ideal height.
 
Back
Top Bottom