Tech
Forums
Jobs
Books
Events
Videos
Live
More
Interviews
Certification
Training
Career
Members
News
Blogs
Contribute
An Article
A Blog
A Video
An Ebook
An Interview Question
Register
Login
1
Answer
Draw Canvas line problem
Adnan
7y
210
1
Reply
Hi folks,
I have a created a Canvas line. here is my source code:
using
System;
namespace
DemoCanvas
{
public
class
Canvas
{
//char[][] canvasArray;
char
[,] canvasArray;
int
w, h;
public
Canvas(
int
w,
int
h)
{
h += 2;
w += 2;
this
.w = w;
this
.h = h;
canvasArray =
new
char
[h,w];
drawLine(0, 0,
this
.w - 1, 0,
'-'
);
drawLine(0,
this
.h - 1,
this
.w - 1,
this
.h - 1,
'-'
);
drawLine(0, 1, 0,
this
.h - 2,
'|'
);
drawLine(
this
.w - 1, 1,
this
.w - 1,
this
.h - 2,
'|'
);
}
public
void
bucketFill(
int
x,
int
y,
char
mchar)
{
if
((
int
)canvasArray[y, x] != 0)
{
return
;
}
if
(x > 0 || x <
this
.h || y > 0 || y <
this
.w)
{
if
((
int
)canvasArray[y, x] == 0)
canvasArray[y, x] = mchar;
bucketFill(x + 1, y, mchar);
bucketFill(x - 1, y, mchar);
bucketFill(x, y - 1, mchar);
bucketFill(x, y + 1, mchar);
}
}
public
void
drawLine(
int
x1,
int
y1,
int
x2,
int
y2,
char
mchar)
{
for
(
int
i = y1; i <= y2; i++)
{
for
(
int
j = x1; j <= x2; j++)
{
canvasArray[i, j] = mchar;
}
}
}
public
void
drawRectangle(
int
x1,
int
y1,
int
x2,
int
y2,
char
mchar)
{
drawLine(x1, y1, x2, y1, mchar);
drawLine(x1, y1, x1, y2, mchar);
drawLine(x2, y1, x2, y2, mchar);
drawLine(x1, y2, x2, y2, mchar);
}
public
void
render()
{
for
(
int
i = 0; i <
this
.h; i++)
{
for
(
int
j = 0; j <
this
.w; j++)
{
Console.WriteLine(canvasArray[i, j]);
}
//Console.WriteLine();
}
}
}
class
Program
{
static
void
Main(
string
[] args)
{
string
command =
""
;
while
(command !=
"Q"
)
{
Console.WriteLine(
"Enter Your Command:(E.g C 4 2) "
);
command = Console.ReadLine();
draw(command);
}
Console.WriteLine(
"Program Exited!"
);
}
static
Canvas canvas;
private
static
void
draw(String command)
{
char
ch = command[0];
String[] cmd;
switch
(ch)
{
case
'C'
:
cmd = command.Split(
' '
);
canvas =
new
Canvas(
int
.Parse(cmd[1]),
int
.Parse(cmd[2]));
canvas.render();
break
;
case
'L'
:
cmd = command.Split(
' '
);
if
(canvas ==
null
)
{
Console.Error.WriteLine(
"Please draw a canvas first"
);
return
;
}
canvas.drawLine(
int
.Parse(cmd[1]),
int
.Parse(cmd[2]),
int
.Parse(cmd[3]),
int
.Parse(cmd[4]),
'X'
);
canvas.render();
break
;
case
'R'
:
cmd = command.Split(
' '
);
if
(canvas ==
null
)
{
Console.Error.WriteLine(
"Please draw a canvas first"
);
return
;
}
canvas.drawRectangle(
int
.Parse(cmd[1]),
int
.Parse(cmd[2]),
int
.Parse(cmd[3]),
int
.Parse(cmd[4]),
'X'
);
canvas.render();
break
;
case
'B'
:
cmd = command.Split(
' '
);
if
(canvas ==
null
)
{
Console.Error.WriteLine(
"Please draw a canvas first"
);
return
;
}
canvas.bucketFill(
int
.Parse(cmd[1]),
int
.Parse(cmd[2]), cmd[3][0]);
canvas.render();
break
;
}
}
}
}
Please copy/paste and run it.
Where 4 is for Width and 2 is for height
I want to display output:
_ _
| |
| |
| |
|_ _ |
I am waiting for your response.
Thanks in Advance
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
read data from text box to text file continuousl when enter
How to check whether txt file is corrupt or not in c#.net ?