jeudi 2 avril 2015

drawing line without using drawLine method in c#

Here is my code and I want to implement DDA algorithm without using drawLine method in c# . I tried to use PutPixel method but it did not work . There is nothing in my window. Is there any way to draw line without using drawLine method in c# ?



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 Proje1B
{
public partial class Form1 : Form
{
public Boolean drawingFlag = false;
public Point ilk, ikinci;

Graphics grafik;

public Form1()
{
InitializeComponent();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
grafik = e.Graphics;

DDACiz(ilk.X, ilk.Y, ikinci.X, ikinci.Y, grafik, Color.DarkRed);


}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (!drawingFlag)
{
drawingFlag = true;
ilk = e.Location;
}
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
drawingFlag = false;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (drawingFlag)
{
ikinci = e.Location;
Invalidate();
}
}
void PutPixel(Graphics g, int x, int y, Color c) // sadece bir pixel icin
{
System.Drawing.Bitmap bm = new System.Drawing.Bitmap(10, 10);
bm.SetPixel(0, 0, Color.DarkRed);
g.DrawImageUnscaled(bm, x, y);
}

void DDACiz(int x1, int y1, int x2, int y2,Graphics grafik, Color renk)
{

int PikselSayisi;

int dx, dy;
float x, xFark;
float y, yFark;

dx = x2 - x1;
dy = y2 - y1;

PikselSayisi = Math.Abs(dx) > Math.Abs(dy) ? Math.Abs(dx) : Math.Abs(dy);

xFark = (float)dx / (float)PikselSayisi;
yFark = (float)dy / (float)PikselSayisi;

x = (float)x1;
y = (float)y1;

while (PikselSayisi!=0)
{
PutPixel(grafik,(int)Math.Floor(x + 0.5F),(int) Math.Floor(y + 0.5f),renk);
x += xFark;
y += yFark;
PikselSayisi--;
}
}
}
}

Aucun commentaire:

Enregistrer un commentaire