A
A
Alexey2019-12-24 13:06:19
Algorithms
Alexey, 2019-12-24 13:06:19

How to correctly implement an application using the Bresenham and Luke algorithms to solve the problem of drawing letters?

Hello! There is a task:
Draw the first letter of the student's last name using the procedures for generating segments according to the Luke method.
Draw the last letter of the student's name using the procedures for generating segments according to the Bresenham method.
The letters are given by the coordinates of the ends of the segments that form them, in symbolic matrices of 50x80 points.
Display letters horizontally, side by side, in the center of the screen, separated by a "space" (an empty character matrix). To do this, use the procedure for converting the coordinates of the ends of segments of symbolic matrices into screen coordinates, depending on the video mode used. Colors for displaying letters are different (any).
Wrote part of the code in C #, drew the letter Y. Please, look, is it correct? confuses the condition about the procedure for converting the coordinates of the ends of the segments of character matrices into screen coordinates, depending on the video mode ...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //Метод, устанавливающий пиксел на форме с заданными цветом и прозрачностью
        private static void PutPixel(Graphics g, Color col, int x, int y, int alpha)
        {
            g.FillRectangle(new SolidBrush(Color.FromArgb(alpha, col)), x, y, 1, 1);
        }

        //Статический метод, реализующий отрисовку 4-связной линии по алгоритму Брезенхема
        static public void Bresenham4Line(Graphics g, Color clr, int x0, int y0, int x1, int y1)
        {
            //Изменения координат
            int dx = (x1 > x0) ? (x1 - x0) : (x0 - x1);
            int dy = (y1 > y0) ? (y1 - y0) : (y0 - y1);
            //Направление приращения
            int sx = (x1 >= x0) ? (1) : (-1);
            int sy = (y1 >= y0) ? (1) : (-1);

            if (dy < dx)
            {
                int d = (dy << 1) - dx;
                int d1 = dy << 1;
                int d2 = (dy - dx) << 1;
                PutPixel(g, clr, x0, y0, 255);
                int x = x0 + sx;
                int y = y0;
                for (int i = 1; i <= dx; i++)
                {
                    if (d > 0)
                    {
                        d += d2;
                        y += sy;
                    }
                    else
                        d += d1;
                    PutPixel(g, clr, x, y, 255);
                    x++;
                }
            }
            else
            {
                int d = (dx << 1) - dy;
                int d1 = dx << 1;
                int d2 = (dx - dy) << 1;
                PutPixel(g, clr, x0, y0, 255);
                int x = x0;
                int y = y0 + sy;
                for (int i = 1; i <= dy; i++)
                {
                    if (d > 0)
                    {
                        d += d2;
                        x += sx;
                    }
                    else
                        d += d1;
                    PutPixel(g, clr, x, y, 255);
                    y++;
                }
            }
        }
        

        private void button1_Click(object sender, EventArgs e)
        {
            Graphics g = pictureBox1.CreateGraphics();
            Bresenham4Line(g, Color.Black, Width/2-25,Height/2-40, Width/2-25, Height / 2 + 40);
            Bresenham4Line(g, Color.Black, Width / 2 + 25, Height / 2 - 40,
                Width / 2 - 25, Height / 2 + 40);
            Bresenham4Line(g, Color.Black, Width / 2 + 25, Height / 2 - 40,
                Width / 2 + 25, Height / 2 + 40);
            Bresenham4Line(g, Color.Black, Width / 2 - 25, Height / 2 - 60,
                Width / 2 , Height / 2 - 50);
            Bresenham4Line(g, Color.Black, Width / 2, Height / 2 - 50,
                Width / 2 + 25, Height / 2 - 60);

        }
    }
}

5e01fccab421a810662710.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2019-12-24
@Rsa97

But here's the question - what is meant by the task, that the coordinates of the ends of the segments that form the letters should be displayed in symbolic matrices of 50 * 80 points?
This means that each letter is drawn in a 50x80 pixel rectangle. Accordingly, the coordinates of the ends of the segments will be in the range 0-49 horizontally and 0-79 vertically.
And what does it mean that the letters are displayed horizontally, side by side in the center of the screen, through a "space" (an empty character matrix), how is it?
But just like this: letter space letter. The center of the space must match the center of the screen.
In general, it is better to ask such questions to your teacher.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question