Created
November 20, 2012 07:46
-
-
Save unsuthee/4116627 to your computer and use it in GitHub Desktop.
UVA 142 : Mouse Clicks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <climits> | |
#include <cstdio> | |
#include <iomanip> | |
using namespace std; | |
class Rect | |
{ | |
public: | |
Rect(int left, int top, int right, int bottom): | |
x1(left), x2(right), y1(top), y2(bottom) | |
{ | |
} | |
bool isInside(int px, int py) const | |
{ | |
return (px >= x1 && px <= x2 && py >= y1 && py <= y2); | |
} | |
int x1; | |
int x2; | |
int y1; | |
int y2; | |
}; | |
class Icon | |
{ | |
public: | |
Icon(int px, int py): x(px), y(py), visible(true) | |
{ | |
} | |
int distance(int px, int py) | |
{ | |
return (x-px)*(x-px)+(y-py)*(y-py); | |
} | |
bool visible; | |
int x; | |
int y; | |
}; | |
class Window | |
{ | |
public: | |
Window():m_bRemoveHiddenIcon(false) | |
{ | |
} | |
void InsertRect(int l, int t, int r, int b) | |
{ | |
char id = static_cast<char>(m_rects.size() + 65); | |
Rect rect(l,t,r,b); | |
m_rects.push_back(rect); | |
} | |
void InsertIcon(int px, int py) | |
{ | |
int id = (int) m_icons.size() + 1; | |
Icon icon(px,py); | |
m_icons.push_back(icon); | |
} | |
bool hasRemoveHiddenIcons() const | |
{ | |
return m_bRemoveHiddenIcon; | |
} | |
void RemoveHiddenIcons() | |
{ | |
for (size_t i=0; i<m_icons.size(); ++i) | |
{ | |
for (size_t j=0; j<m_rects.size(); ++j) | |
{ | |
if (m_rects[j].isInside(m_icons[i].x, m_icons[i].y)) | |
{ | |
m_icons[i].visible = false; | |
break; | |
} | |
} | |
} | |
m_bRemoveHiddenIcon = true; | |
} | |
void PrintSelection(int mx, int my) | |
{ | |
for (int i=(int)m_rects.size()-1; i>=0; --i) | |
{ | |
if (m_rects[i].isInside(mx,my)) | |
{ | |
cout << (char)(i+65) << endl; | |
return; | |
} | |
} | |
// there is always an icon | |
std::vector<int> iconId; | |
int dist = INT_MAX; | |
for (size_t i=0; i<m_icons.size(); ++i) | |
{ | |
if (!m_icons[i].visible) | |
continue; | |
int newDist = m_icons[i].distance(mx,my); | |
if (newDist == dist) | |
iconId.push_back(i+1); | |
else if (newDist < dist) { | |
dist = newDist; | |
iconId.clear(); | |
iconId.push_back(i+1); | |
} | |
} | |
for (size_t i=0; i<iconId.size(); i++) { | |
cout << setw(3) << iconId[i]; | |
} | |
cout << endl; | |
} | |
bool m_bRemoveHiddenIcon; | |
std::vector<Rect> m_rects; | |
std::vector<Icon> m_icons; // only visible icons | |
}; | |
int main(int argc, const char * argv[]) | |
{ | |
Window w; | |
bool doneProcessing = false; | |
char t; | |
char line[255]; | |
while (gets(line)) { | |
switch(line[0]) { | |
case '#': | |
return 0; | |
case 'I': | |
{ | |
int x, y; | |
sscanf(&line[1],"%d %d",&x,&y); | |
w.InsertIcon(x,y); | |
} | |
break; | |
case 'R': | |
{ | |
int l, t, r, b; | |
sscanf(&line[1],"%d %d %d %d", &l, &t, &r, &b); | |
w.InsertRect(l, t, r, b); | |
} | |
break; | |
case 'M': | |
{ | |
if (!w.hasRemoveHiddenIcons()) { | |
w.RemoveHiddenIcons(); | |
} | |
int x, y; | |
sscanf(&line[1],"%d %d",&x,&y); | |
w.PrintSelection(x,y); | |
} | |
break; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment