from PyQt4 import QtGui QtCore import math from PyQt4 QtGui import QGr

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from PyQt4 import QtGui, QtCore
import math
from PyQt4.QtGui import QGraphicsEllipseItem
import time
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.view = View(self)
#self.button = QtGui.QPushButton('Clear view', self)
#self.button.clicked.connect(self.handleClearView)
# self.button.clicked.connect(self.handlePoints)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.view)
#layout.addWidget(self.button)
def handleClearView(self):
self.view.scene().clear()
class View(QtGui.QGraphicsView):
def __init__(self, parent):
QtGui.QGraphicsView.__init__(self, parent)
self.setScene(QtGui.QGraphicsScene(self))
self.setSceneRect(QtCore.QRectF(self.viewport().rect()))
self.listOfPoints = []
self.listOfClusters = []
self.counter = 0;
self.choosen = 0;
self.Point = QtGui.QPushButton('Point', self)
self.Claster = QtGui.QPushButton('Claster', self)
self.ClusteringEuclid = QtGui.QPushButton('Clustering Euclid', self)
self.ClusteringManhattan = QtGui.QPushButton('Clustering Manhattan', self)
self.Claster.move(83,0)
self.ClusteringEuclid.move(166,0)
self.ClusteringManhattan.move(292,0)
self.Point.clicked.connect(self.handleButton2)
self.Claster.clicked.connect(self.handleButton3)
self.ClusteringEuclid.clicked.connect(self.handleButton4)
self.ClusteringManhattan.clicked.connect(self.handleButton5)
self.colorList = [QtCore.Qt.red, QtCore.Qt.green, QtCore.Qt.black, QtCore.Qt.magenta, QtCore.Qt.yellow, QtCore.Qt.blue, QtCore.Qt.gray, QtCore.Qt.cyan, QtCore.Qt.darkRed]
def handleButton2(self):
self.counter = 0;
print(self.counter)
def handleButton3(self):
self.counter = 1;
print(self.counter)
def handleButton4(self):
self.counter = 2;
self.choosen = 1;
def handleButton5(self):
self.counter = 2;
def mousePressEvent(self, event):
self._start = event.pos()
def mouseReleaseEvent(self, event):
start = QtCore.QPointF(self.mapToScene(self._start))
if self.counter == 0:
point = start
ellipsec = self.scene().addEllipse(point.x(),point.y(),4,4)
ellipsec.setBrush(QtCore.Qt.gray)
ellipsec.setPen(QtCore.Qt.gray)
self.listOfPoints.append(point)
elif self.counter == 1:
point = start
ellipsec = self.scene().addEllipse(point.x()-5,point.y()-5,10,10)
ellipsec.setBrush(QtCore.Qt.gray)
ellipsec.setPen(QtCore.Qt.gray)
self.listOfClusters.append(point)
elif self.counter == 2:
while (True):
breakPoint = 0
Array = [[] for y in range(len(self.listOfClusters))]
for i in range(len(self.listOfPoints)):
min = math.sqrt(math.pow((self.listOfClusters[0].x() - self.listOfPoints[i].x()),2) + math.pow((self.listOfClusters[0].y() - self.listOfPoints[i].y()),2))
index = 0;
for j in range(len(self.listOfClusters)):
if self.choosen == 1:
distance = math.sqrt(math.pow((self.listOfClusters[j].x() - self.listOfPoints[i].x()),2) + math.pow((self.listOfClusters[j].y() - self.listOfPoints[i].y()),2))
else:
distance = (math.fabs(self.listOfClusters[j].x()-self.listOfPoints[i].x()) + math.fabs(self.listOfClusters[j].y() - self.listOfPoints[i].y()))
if min > distance:
min = distance
index = j;
Array[index].append(self.listOfPoints[i])
self.scene().clear()
for i in range(len(self.listOfClusters)):
ellipseColor = self.scene().addEllipse(self.listOfClusters[i].x(),self.listOfClusters[i].y(), 10, 10)
ellipseColor.setPen(self.colorList[i])
ellipseColor.setBrush(self.colorList[i])
for j in range(len(Array[i])):
pointColor = self.scene().addEllipse(Array[i][j].x(),Array[i][j].y(), 4, 4)
pointColor.setPen(self.colorList[i])
pointColor.setBrush(self.colorList[i])
lineColor = self.scene().addLine(self.listOfClusters[i].x()+5,self.listOfClusters[i].y()+5,Array[i][j].x()+2,Array[i][j].y()+2, self.colorList[i])
for i in range (len(self.listOfClusters)):
newPointX = 0
newPointY = 0
for j in range (len(Array[i])):
newPointX += Array[i][j].x()
newPointY += Array[i][j].y()
if len(Array[i]) != 0:
newX = newPointX / len(Array[i])
newY = newPointY / len(Array[i])
newPoint = QtCore.QPoint(newX, newY)
if (newPoint.x() == self.listOfClusters[i].x()) & (newPoint.y() == self.listOfClusters[i].y()):
breakPoint += 1;
self.listOfClusters[i] = newPoint
if breakPoint == len(self.listOfClusters):
print("end")
break;
else:
self.scene().clear()
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.resize(640, 480)
window.show()
sys.exit(app.exec_())