<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: editor code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Tue, 07 Oct 2008 11:12:23 GMT</pubDate>
    <description>DZone Snippets: editor code</description>
    <item>
      <title>A solution for the "Graphical Editor" problem</title>
      <link>http://snippets.dzone.com/posts/show/5245</link>
      <description>A solution for the "Graphical Editor" problem.&lt;br /&gt; &lt;br /&gt;Problem description:&lt;br /&gt;&lt;a href="http://icpcres.ecs.baylor.edu/onlinejudge/external/102/10267.html"&gt;http://icpcres.ecs.baylor.edu/onlinejudge/external/102/10267.html&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Author: &lt;a href="http://joanatrindade.wikidot.com"&gt;Joana Matos Fonseca da Trindade&lt;/a&gt;&lt;br /&gt;Date: 2008.03.12&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/* &lt;br /&gt; * Solution for the "Graphical Editor" problem.&lt;br /&gt; * UVa ID: 10267&lt;br /&gt; */&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;#define MAX 250&lt;br /&gt;#define OFFSET 1&lt;br /&gt;#define DOS_NAME 12&lt;br /&gt;&lt;br /&gt;/* global image bounds */&lt;br /&gt;int n, m;&lt;br /&gt;&lt;br /&gt;/* fills a rectangle with the specified color */&lt;br /&gt;int fillRectangle(int m_ini, int n_ini, int m_end, int n_end, char color, char pTable[][MAX+OFFSET]) {&lt;br /&gt;	int i, j;&lt;br /&gt;	for (i = n_ini; i &lt;= n_end; i++) {&lt;br /&gt;		for (j = m_ini; j &lt;= m_end; j++) {&lt;br /&gt;			pTable[i][j] = color;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* fills a region R with the specified color */&lt;br /&gt;int fillRegion(int x, int y, char oldColor, char newColor, char pTable[][MAX+OFFSET]) {	&lt;br /&gt;	/* (x,y) is in region R */&lt;br /&gt;	pTable[y][x] = newColor;&lt;br /&gt;	&lt;br /&gt;	/* recursively check all 4 directions for neighbours of (x,y) with same color */&lt;br /&gt;	if ((pTable[y][x-1] == oldColor) &amp;&amp; (x &gt; OFFSET)) {         &lt;br /&gt;		fillRegion(x-1, y, oldColor, newColor, pTable);&lt;br /&gt;	}&lt;br /&gt;	if ((pTable[y][x+1] == oldColor) &amp;&amp; (x &lt; m)) {       &lt;br /&gt;		fillRegion(x+1, y, oldColor, newColor, pTable);&lt;br /&gt;	}&lt;br /&gt;	if ((pTable[y-1][x] == oldColor) &amp;&amp; (y &gt; OFFSET)) {        &lt;br /&gt;		fillRegion(x, y-1, oldColor, newColor, pTable);&lt;br /&gt;	}&lt;br /&gt;	if ((pTable[y+1][x] == oldColor) &amp;&amp; (y &lt; n)) {        &lt;br /&gt;		fillRegion(x, y+1, oldColor, newColor, pTable);&lt;br /&gt;	}&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* outputs the image */&lt;br /&gt;int printImage(int m, int n, char pTable[][MAX+OFFSET]) {&lt;br /&gt;	int i, j;	&lt;br /&gt;	for (i = OFFSET; i &lt; n+OFFSET; i++) {&lt;br /&gt;		for (j = OFFSET; j &lt; m+OFFSET; j++ ) {&lt;br /&gt;			printf("%c", pTable[i][j]);&lt;br /&gt;		}&lt;br /&gt;		printf("\n");&lt;br /&gt;	}&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* main */&lt;br /&gt;int main (int argc, const char * argv[]) {&lt;br /&gt;	/* the image */&lt;br /&gt;	char image[MAX+OFFSET][MAX+OFFSET];&lt;br /&gt;&lt;br /&gt;	/* editor command */&lt;br /&gt;	char command;&lt;br /&gt;	&lt;br /&gt;	/* coords */&lt;br /&gt;	int x1, x2, y1, y2, tmp;&lt;br /&gt;	&lt;br /&gt;	/* colors */&lt;br /&gt;	char color, oldColor;&lt;br /&gt;	&lt;br /&gt;	/* filename */&lt;br /&gt;	char filename[DOS_NAME+1];&lt;br /&gt;			&lt;br /&gt;	while(scanf("%c", &amp;command) != EOF) {		&lt;br /&gt;		/* X, terminates the session */&lt;br /&gt;		if (command == 'X') {&lt;br /&gt;			return 0;&lt;br /&gt;		}		&lt;br /&gt;		switch (command) {&lt;br /&gt;			/* create image */&lt;br /&gt;			case 'I' :&lt;br /&gt;				scanf("%d %d", &amp;m, &amp;n);&lt;br /&gt;				fillRectangle(1, 1, m, n, 'O', image);&lt;br /&gt;				break;&lt;br /&gt;			&lt;br /&gt;			/* clear image */&lt;br /&gt;			case 'C' :&lt;br /&gt;				fillRectangle(1, 1, m, n, 'O', image);&lt;br /&gt;				break;&lt;br /&gt;			&lt;br /&gt;			/* colors a pixel */&lt;br /&gt;			case 'L' :&lt;br /&gt;				scanf("%d %d %c", &amp;x1, &amp;y1, &amp;color);&lt;br /&gt;				image[y1][x1] = color;&lt;br /&gt;				break;&lt;br /&gt;			&lt;br /&gt;			/* draw vertical segment */&lt;br /&gt;			case 'V' :&lt;br /&gt;				scanf("%d %d %d %c", &amp;x1, &amp;y1, &amp;y2, &amp;color);&lt;br /&gt;				if (y2 &gt;= y1)&lt;br /&gt;					fillRectangle(x1, y1, x1, y2, color, image);&lt;br /&gt;				else&lt;br /&gt;					fillRectangle(x1, y2, x1, y1, color, image);&lt;br /&gt;				break;&lt;br /&gt;			&lt;br /&gt;			/* draw horizontal segment */&lt;br /&gt;			case 'H' : &lt;br /&gt;				scanf("%d %d %d %c", &amp;x1, &amp;x2, &amp;y1, &amp;color);&lt;br /&gt;				if (x2 &gt;= x1)&lt;br /&gt;					fillRectangle(x1, y1, x2, y1, color, image);&lt;br /&gt;				else&lt;br /&gt;					fillRectangle(x2, y1, x1, y1, color, image);&lt;br /&gt;				break;&lt;br /&gt;			&lt;br /&gt;			/* draw rectangle */&lt;br /&gt;			case 'K' : &lt;br /&gt;				scanf("%d %d %d %d %c", &amp;x1, &amp;y1, &amp;x2, &amp;y2, &amp;color);&lt;br /&gt;				if (x1 &gt;= x2) {&lt;br /&gt;					tmp = x1;&lt;br /&gt;					x1 = x2;&lt;br /&gt;					x2 = tmp;&lt;br /&gt;				}&lt;br /&gt;				if (y1 &gt;= y2) {&lt;br /&gt;					tmp = y1;&lt;br /&gt;					y1 = y2;&lt;br /&gt;					y2 = tmp;&lt;br /&gt;				}&lt;br /&gt;				fillRectangle(x1, y1, x2, y2, color, image);&lt;br /&gt;				break;&lt;br /&gt;			&lt;br /&gt;			/* fill */&lt;br /&gt;			case 'F' :&lt;br /&gt;				scanf("%d %d %c", &amp;x1, &amp;y1, &amp;color);&lt;br /&gt;				oldColor = image[y1][x1];&lt;br /&gt;				if (oldColor != color) {&lt;br /&gt;					fillRegion(x1, y1, oldColor, color, image);&lt;br /&gt;				}&lt;br /&gt;				break;&lt;br /&gt;&lt;br /&gt;			/* fill */&lt;br /&gt;			case 'S' :&lt;br /&gt;				scanf("%s", &amp;filename);&lt;br /&gt;				printf("%s\n", filename);&lt;br /&gt;				printImage(m, n, image);&lt;br /&gt;				break;			&lt;br /&gt;		&lt;br /&gt;			default: &lt;br /&gt;				break;&lt;br /&gt;		}		&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 18 Mar 2008 09:23:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5245</guid>
      <author>jmftrindade (Joana M. F. da Trindade)</author>
    </item>
  </channel>
</rss>
