From 5e9d6ff91ae63c7dd9baaa2d9a03ca63ca5f5d74 Mon Sep 17 00:00:00 2001 From: Denis Klimenko Date: Sun, 2 Dec 2012 16:16:47 +0300 Subject: [PATCH 1/3] Done calculating incircle points and drawing incircle. For the present redrawing working incorrectly --- components/geometry/base/geom_modes.py | 87 +++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/components/geometry/base/geom_modes.py b/components/geometry/base/geom_modes.py index bbab719..4599295 100644 --- a/components/geometry/base/geom_modes.py +++ b/components/geometry/base/geom_modes.py @@ -44,6 +44,7 @@ import suit.cf.utils as comutils import suit.core.render.engine as render_engine import geom_controls +import math class GeometryEditMode(BaseEditMode): @@ -357,7 +358,91 @@ def _onKeyPressed(self, _evt): if isinstance(obj, (GeometryTriangle, GeometryQuadrangle)): self.state = GeometryEditMode.ES_PerimeterChange self.perimetr_changer = TextInput(obj, self._perimeter_change_callback, obj.getPropertyValue(GeometryAbstractObject.PropPerimeter)) - + + if key == ois.KC_V: + selected = self._logic._getSheet().getSelected() + if len(selected) == 1: + obj = selected[0] + if isinstance(obj, (GeometryTriangle)): + + triangleSides = obj.getSides() + + # calculate triangle vertex coordinates + pointA_x = render_engine.pos3dTo2dWindow(triangleSides[0].getEnd().getPosition())[0] + pointA_y = render_engine.pos3dTo2dWindow(triangleSides[0].getEnd().getPosition())[1] + pointB_x = render_engine.pos3dTo2dWindow(triangleSides[1].getEnd().getPosition())[0] + pointB_y = render_engine.pos3dTo2dWindow(triangleSides[1].getEnd().getPosition())[1] + pointC_x = render_engine.pos3dTo2dWindow(triangleSides[2].getEnd().getPosition())[0] + pointC_y = render_engine.pos3dTo2dWindow(triangleSides[2].getEnd().getPosition())[1] + + # calculate triangle sides length + sideA_length = math.sqrt(math.pow(pointC_x - pointB_x, 2) + math.pow(pointC_y - pointB_y, 2)) + sideB_length = math.sqrt(math.pow(pointA_x - pointC_x, 2) + math.pow(pointA_y - pointC_y, 2)) + sideC_length = math.sqrt(math.pow(pointA_x - pointB_x, 2) + math.pow(pointA_y - pointB_y, 2)) + + # calculate incenter coordinates + # formula: X = (BC * Xa + AC * Xb + AB * Xc) / (AB + BC + AC) + # Y = (BC * Ya + AC * Yb + AB * Yc) / (AB + BC + AC) + # where X, Y - incenter coordinates + # AB, BC, AC - triangle sides + # Xa, Xb, Xc - triangle A, B, C vertexes X coordinates + # Ya, Yb, Yc - triangle A, B, C vertexes Y coordinates + incenter_x = (sideA_length * pointA_x + sideB_length * pointB_x + sideC_length * pointC_x) / (sideA_length + sideB_length + sideC_length) + incenter_y = (sideA_length * pointA_y + sideB_length * pointB_y + sideC_length * pointC_y) / (sideA_length + sideB_length + sideC_length) + + # calculate half perimeter + halfPerimeter = (sideA_length + sideB_length + sideC_length) / 2 + + # calculate incircle radius + # formula r = ((p - a)*(p - b)*(p - c) / p) ^ 1/2 + # where r - radius + # p - half perimeter + # a, b, c - triangle sides + radius = math.sqrt((halfPerimeter - sideA_length) * (halfPerimeter - sideB_length) * (halfPerimeter * sideC_length) / halfPerimeter) + + # calculate tangent point coordinates (on AC triangle side) + # formula X = (Xa - Xc) * (p - AB) / AC + Xc + # Y = (Ya - Yc) * (p - AB) / AC + Yc + # where X, Y - tangent point coordinates + # Xa, Xc - triangle A, C vertexes X coordinates + # Ya, Yc - triangle A, C vertexes Y coordinates + # p - half perimeter + tangentPoint_x = (pointA_x - pointC_x) * (halfPerimeter - sideC_length) / sideB_length + pointC_x + tangentPoint_y = (pointA_y - pointC_y) * (halfPerimeter - sideC_length) / sideB_length + pointC_y + + # create center point + incenterPointCoord = incenter_x, incenter_y + incenter = self._logic.createPoint(incenterPointCoord) + sheet = self._logic._getSheet() + sheet.addChild(incenter) + incenter._updateView() + + # create tangent point + tangentPointCoord = tangentPoint_x, tangentPoint_y + + # get tangent point base side line + sideObjects = self._logic._getSheet()._getObjectsUnderMouse(True, True, tangentPointCoord) + line = comutils._getFirstObjectTypeFromList(sideObjects, [GeometryLineSection]) + + tangent = self._logic.createPoint(tangentPointCoord) + sheet = self._logic._getSheet() + sheet.addChild(tangent) + + # add tangent point to base side line childs + line.addPoint(tangent, line._calculatePointRelPosition(render_engine.pos2dTo3dIsoPos(tangentPointCoord))) + + tangent._updateView() + + _selected = incenter, tangent + + # draw incircle + self.active_object = self._logic.createCircle() + if self.active_object.makeBasedOnObjects(_selected): + self._logic._getSheet().addChild(self.active_object) + else: + self.active_object.delete() + self.active_object = None + return False def _onKeyReleased(self, _evt): From 03926c391930f6932c8296e9937ec3f50f4d85a3 Mon Sep 17 00:00:00 2001 From: Denis Date: Tue, 11 Dec 2012 12:20:31 +0300 Subject: [PATCH 2/3] Done incircle redrawing --- components/geometry/base/geom_modes.py | 58 ++------------- components/geometry/base/geom_objects.py | 90 +++++++++++++++++++++++- 2 files changed, 95 insertions(+), 53 deletions(-) diff --git a/components/geometry/base/geom_modes.py b/components/geometry/base/geom_modes.py index 4599295..d3203de 100644 --- a/components/geometry/base/geom_modes.py +++ b/components/geometry/base/geom_modes.py @@ -361,64 +361,20 @@ def _onKeyPressed(self, _evt): if key == ois.KC_V: selected = self._logic._getSheet().getSelected() - if len(selected) == 1: - obj = selected[0] + for obj in selected: if isinstance(obj, (GeometryTriangle)): - triangleSides = obj.getSides() - - # calculate triangle vertex coordinates - pointA_x = render_engine.pos3dTo2dWindow(triangleSides[0].getEnd().getPosition())[0] - pointA_y = render_engine.pos3dTo2dWindow(triangleSides[0].getEnd().getPosition())[1] - pointB_x = render_engine.pos3dTo2dWindow(triangleSides[1].getEnd().getPosition())[0] - pointB_y = render_engine.pos3dTo2dWindow(triangleSides[1].getEnd().getPosition())[1] - pointC_x = render_engine.pos3dTo2dWindow(triangleSides[2].getEnd().getPosition())[0] - pointC_y = render_engine.pos3dTo2dWindow(triangleSides[2].getEnd().getPosition())[1] - - # calculate triangle sides length - sideA_length = math.sqrt(math.pow(pointC_x - pointB_x, 2) + math.pow(pointC_y - pointB_y, 2)) - sideB_length = math.sqrt(math.pow(pointA_x - pointC_x, 2) + math.pow(pointA_y - pointC_y, 2)) - sideC_length = math.sqrt(math.pow(pointA_x - pointB_x, 2) + math.pow(pointA_y - pointB_y, 2)) - - # calculate incenter coordinates - # formula: X = (BC * Xa + AC * Xb + AB * Xc) / (AB + BC + AC) - # Y = (BC * Ya + AC * Yb + AB * Yc) / (AB + BC + AC) - # where X, Y - incenter coordinates - # AB, BC, AC - triangle sides - # Xa, Xb, Xc - triangle A, B, C vertexes X coordinates - # Ya, Yb, Yc - triangle A, B, C vertexes Y coordinates - incenter_x = (sideA_length * pointA_x + sideB_length * pointB_x + sideC_length * pointC_x) / (sideA_length + sideB_length + sideC_length) - incenter_y = (sideA_length * pointA_y + sideB_length * pointB_y + sideC_length * pointC_y) / (sideA_length + sideB_length + sideC_length) - - # calculate half perimeter - halfPerimeter = (sideA_length + sideB_length + sideC_length) / 2 - - # calculate incircle radius - # formula r = ((p - a)*(p - b)*(p - c) / p) ^ 1/2 - # where r - radius - # p - half perimeter - # a, b, c - triangle sides - radius = math.sqrt((halfPerimeter - sideA_length) * (halfPerimeter - sideB_length) * (halfPerimeter * sideC_length) / halfPerimeter) - - # calculate tangent point coordinates (on AC triangle side) - # formula X = (Xa - Xc) * (p - AB) / AC + Xc - # Y = (Ya - Yc) * (p - AB) / AC + Yc - # where X, Y - tangent point coordinates - # Xa, Xc - triangle A, C vertexes X coordinates - # Ya, Yc - triangle A, C vertexes Y coordinates - # p - half perimeter - tangentPoint_x = (pointA_x - pointC_x) * (halfPerimeter - sideC_length) / sideB_length + pointC_x - tangentPoint_y = (pointA_y - pointC_y) * (halfPerimeter - sideC_length) / sideB_length + pointC_y + incirclePoints = obj._calculateIncirclePoints() # create center point - incenterPointCoord = incenter_x, incenter_y + incenterPointCoord = incirclePoints[0] incenter = self._logic.createPoint(incenterPointCoord) sheet = self._logic._getSheet() sheet.addChild(incenter) incenter._updateView() # create tangent point - tangentPointCoord = tangentPoint_x, tangentPoint_y + tangentPointCoord = incirclePoints[1] # get tangent point base side line sideObjects = self._logic._getSheet()._getObjectsUnderMouse(True, True, tangentPointCoord) @@ -428,17 +384,17 @@ def _onKeyPressed(self, _evt): sheet = self._logic._getSheet() sheet.addChild(tangent) - # add tangent point to base side line childs - line.addPoint(tangent, line._calculatePointRelPosition(render_engine.pos2dTo3dIsoPos(tangentPointCoord))) - tangent._updateView() _selected = incenter, tangent + obj._setIncenterPoints(_selected) + # draw incircle self.active_object = self._logic.createCircle() if self.active_object.makeBasedOnObjects(_selected): self._logic._getSheet().addChild(self.active_object) + obj._setIsIncircleDrawed(True) else: self.active_object.delete() self.active_object = None diff --git a/components/geometry/base/geom_objects.py b/components/geometry/base/geom_objects.py index 1d81cef..a3bcc91 100644 --- a/components/geometry/base/geom_objects.py +++ b/components/geometry/base/geom_objects.py @@ -919,6 +919,8 @@ def __init__(self): self.__manualObject = None # manual object to store and render geometry self.pts = [] # triangle vertex points self.sides = [] # triangle sides + self.isIncircleDrawed = False #flag showing is incircle shown on objects sheet + self.incirclePoints = [] #turple for incircle points def __del__(self): suit.core.objects.ObjectDepth.__del__(self) @@ -984,6 +986,14 @@ def _update(self, _timeSinceLastFrame): self.__manualObject.triangle(2, 1, 0) self.__manualObject.end() + + # update incircle points position + if self.isIncircleDrawed: + incirclePointsCoordinats = self._calculateIncirclePoints() + incircle = self.incirclePoints[0] + tangent = self.incirclePoints[1] + incircle.setPosition(render_engine.pos2dTo3dIsoPos(incirclePointsCoordinats[0])) + tangent.setPosition(render_engine.pos2dTo3dIsoPos(incirclePointsCoordinats[1])) suit.core.objects.ObjectDepth._update(self, _timeSinceLastFrame) @@ -1117,9 +1127,85 @@ def build_text_idtf(self): return u"%s(%s;%s;%s)" % (u'Треугк', idtf1, idtf2, idtf3) else: - return None + return None + + def _calculateIncirclePoints(self): + """Calculate points for incircle and draw it on objects sheet + """ + + # calculate triangle vertex coordinates + pointA_x = render_engine.pos3dTo2dWindow(self.pts[0].getPosition())[0] + pointA_y = render_engine.pos3dTo2dWindow(self.pts[0].getPosition())[1] + pointB_x = render_engine.pos3dTo2dWindow(self.pts[1].getPosition())[0] + pointB_y = render_engine.pos3dTo2dWindow(self.pts[1].getPosition())[1] + pointC_x = render_engine.pos3dTo2dWindow(self.pts[2].getPosition())[0] + pointC_y = render_engine.pos3dTo2dWindow(self.pts[2].getPosition())[1] + + # calculate triangle sides length + sideA_length = math.sqrt(math.pow(pointC_x - pointB_x, 2) + math.pow(pointC_y - pointB_y, 2)) + sideB_length = math.sqrt(math.pow(pointA_x - pointC_x, 2) + math.pow(pointA_y - pointC_y, 2)) + sideC_length = math.sqrt(math.pow(pointA_x - pointB_x, 2) + math.pow(pointA_y - pointB_y, 2)) + + # calculate incenter coordinates + # formula: X = (BC * Xa + AC * Xb + AB * Xc) / (AB + BC + AC) + # Y = (BC * Ya + AC * Yb + AB * Yc) / (AB + BC + AC) + # where X, Y - incenter coordinates + # AB, BC, AC - triangle sides + # Xa, Xb, Xc - triangle A, B, C vertexes X coordinates + # Ya, Yb, Yc - triangle A, B, C vertexes Y coordinates + incenter_x = (sideA_length * pointA_x + sideB_length * pointB_x + sideC_length * pointC_x) / (sideA_length + sideB_length + sideC_length) + incenter_y = (sideA_length * pointA_y + sideB_length * pointB_y + sideC_length * pointC_y) / (sideA_length + sideB_length + sideC_length) + + # calculate half perimeter + halfPerimeter = (sideA_length + sideB_length + sideC_length) / 2 + + # calculate incircle radius + # formula r = ((p - a)*(p - b)*(p - c) / p) ^ 1/2 + # where r - radius + # p - half perimeter + # a, b, c - triangle sides + radius = math.sqrt((halfPerimeter - sideA_length) * (halfPerimeter - sideB_length) * (halfPerimeter - sideC_length) / halfPerimeter) + + # calculate tangent point coordinates (on AC triangle side) + # formula X = (Xa - Xc) * (p - AB) / AC + Xc + # Y = (Ya - Yc) * (p - AB) / AC + Yc + # where X, Y - tangent point coordinates + # Xa, Xc - triangle A, C vertexes X coordinates + # Ya, Yc - triangle A, C vertexes Y coordinates + # p - half perimeter + tangentPoint_x = (pointA_x - pointC_x) * (halfPerimeter - sideC_length) / sideB_length + pointC_x + tangentPoint_y = (pointA_y - pointC_y) * (halfPerimeter - sideC_length) / sideB_length + pointC_y + + # create center point + incenterPointCoord = incenter_x, incenter_y + + # create tangent point + tangentPointCoord = tangentPoint_x, tangentPoint_y + + return [incenterPointCoord, tangentPointCoord] + + def _setIncenterPoints(self, incirclePoints): + """set list of incircle points + """ + self.incirclePoints = incirclePoints + + def _getIncenterPoints(self): + """get list of incircle points + """ + return self.incirclePoints + + def _setIsIncircleDrawed(self, isIncircleDrawed): + """set flag indicating if incircle drawed + """ + self.isIncircleDrawed = isIncircleDrawed + + def _getIsIncircleDrawed(self): + """get flag indicating if incircle drawed + """ + return self.isIncircleDrawed + - + class GeometryQuadrangle(suit.core.objects.ObjectDepth, GeometryAbstractObject): def __init__(self): From 0bb5260454c41032a87e39ab72e29b478b38bb42 Mon Sep 17 00:00:00 2001 From: Denis Klimenko Date: Fri, 14 Dec 2012 04:05:35 +0300 Subject: [PATCH 3/3] Done disabling incircle redrawing after it manually dislocation --- components/geometry/base/geom_objects.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/components/geometry/base/geom_objects.py b/components/geometry/base/geom_objects.py index a3bcc91..7df6378 100644 --- a/components/geometry/base/geom_objects.py +++ b/components/geometry/base/geom_objects.py @@ -921,6 +921,7 @@ def __init__(self): self.sides = [] # triangle sides self.isIncircleDrawed = False #flag showing is incircle shown on objects sheet self.incirclePoints = [] #turple for incircle points + self.previousIncirclePtsCoordinates = [] # coordinates for incircle points at previous update def __del__(self): suit.core.objects.ObjectDepth.__del__(self) @@ -989,11 +990,17 @@ def _update(self, _timeSinceLastFrame): # update incircle points position if self.isIncircleDrawed: - incirclePointsCoordinats = self._calculateIncirclePoints() - incircle = self.incirclePoints[0] - tangent = self.incirclePoints[1] - incircle.setPosition(render_engine.pos2dTo3dIsoPos(incirclePointsCoordinats[0])) - tangent.setPosition(render_engine.pos2dTo3dIsoPos(incirclePointsCoordinats[1])) + if str(render_engine.pos3dTo2dWindow(self.incirclePoints[0].getPosition())[0]) == self.previousIncirclePtsCoordinates[0][0] \ + and str(render_engine.pos3dTo2dWindow(self.incirclePoints[0].getPosition())[1]) == self.previousIncirclePtsCoordinates[0][1] \ + and str(render_engine.pos3dTo2dWindow(self.incirclePoints[1].getPosition())[0]) == self.previousIncirclePtsCoordinates[1][0] \ + and str(render_engine.pos3dTo2dWindow(self.incirclePoints[1].getPosition())[1]) == self.previousIncirclePtsCoordinates[1][1]: + incirclePointsCoordinats = self._calculateIncirclePoints() + incircle = self.incirclePoints[0] + tangent = self.incirclePoints[1] + incircle.setPosition(render_engine.pos2dTo3dIsoPos(incirclePointsCoordinats[0])) + tangent.setPosition(render_engine.pos2dTo3dIsoPos(incirclePointsCoordinats[1])) + else: + self.isIncircleDrawed = False suit.core.objects.ObjectDepth._update(self, _timeSinceLastFrame) @@ -1182,6 +1189,8 @@ def _calculateIncirclePoints(self): # create tangent point tangentPointCoord = tangentPoint_x, tangentPoint_y + self.previousIncirclePtsCoordinates = [[str(incenterPointCoord[0]).split(".")[0], str(incenterPointCoord[1]).split(".")[0]], [str(tangentPointCoord[0]).split(".")[0], str(tangentPointCoord[1]).split(".")[0]]] + return [incenterPointCoord, tangentPointCoord] def _setIncenterPoints(self, incirclePoints):