Browse Source

Implemented Transforming

Vsevolod Levitan 1 month ago
parent
commit
e27e599b15
6 changed files with 120 additions and 42 deletions
  1. 23 27
      .idea/workspace.xml
  2. 31 4
      src/Circle.kt
  3. 1 2
      src/Main.kt
  4. 34 3
      src/Rect.kt
  5. 31 4
      src/Square.kt
  6. 0 2
      src/Transforming.kt

+ 23 - 27
.idea/workspace.xml

@@ -1,13 +1,16 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
+  <component name="AutoImportSettings">
+    <option name="autoReloadType" value="SELECTIVE" />
+  </component>
   <component name="ChangeListManager">
     <list default="true" id="b9a6b32c-5578-4410-bafa-6b56507d845c" name="Default Changelist" comment="">
-      <change afterPath="$PROJECT_DIR$/src/Circle.kt" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/Circle.kt" beforeDir="false" afterPath="$PROJECT_DIR$/src/Circle.kt" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/src/Main.kt" beforeDir="false" afterPath="$PROJECT_DIR$/src/Main.kt" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/src/Transforming.kt" beforeDir="false" afterPath="$PROJECT_DIR$/src/Transforming.kt" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/Rect.kt" beforeDir="false" afterPath="$PROJECT_DIR$/src/Rect.kt" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/Square.kt" beforeDir="false" afterPath="$PROJECT_DIR$/src/Square.kt" afterDir="false" />
     </list>
-    <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
     <option name="SHOW_DIALOG" value="false" />
     <option name="HIGHLIGHT_CONFLICTS" value="true" />
     <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
@@ -25,24 +28,23 @@
   <component name="Git.Settings">
     <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
   </component>
+  <component name="ProjectColorInfo"><![CDATA[{
+  "associatedIndex": 5
+}]]></component>
   <component name="ProjectId" id="20i3zOracRcUAqzmxlDFJAh5iSe" />
-  <component name="ProjectLevelVcsManager" settingsEditedManually="true" />
-  <component name="PropertiesComponent">
-    <property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
-    <property name="settings.editor.selected.configurable" value="editor.preferences.fonts.default" />
-  </component>
-  <component name="RunDashboard">
-    <option name="ruleStates">
-      <list>
-        <RuleState>
-          <option name="name" value="ConfigurationTypeDashboardGroupingRule" />
-        </RuleState>
-        <RuleState>
-          <option name="name" value="StatusDashboardGroupingRule" />
-        </RuleState>
-      </list>
-    </option>
+  <component name="ProjectViewState">
+    <option name="hideEmptyMiddlePackages" value="true" />
+    <option name="showLibraryContents" value="true" />
   </component>
+  <component name="PropertiesComponent"><![CDATA[{
+  "keyToString": {
+    "RunOnceActivity.ShowReadmeOnStart": "true",
+    "RunOnceActivity.git.unshallow": "true",
+    "git-widget-placeholder": "main",
+    "kotlin-language-version-configured": "true",
+    "last_opened_file_path": "C:/Users/Vsevolod/Repositories/temp/Figures"
+  }
+}]]></component>
   <component name="RunManager">
     <configuration default="true" type="ClojureREPL" factoryName="Local" activateToolWindowBeforeRun="false">
       <setting name="replType" value="NREPL" />
@@ -65,15 +67,9 @@
       <setting name="fixLineNumbers" value="false" />
       <method v="2" />
     </configuration>
-    <configuration name="MainKt" type="JetRunConfigurationType" factoryName="Kotlin" temporary="true" nameIsGenerated="true">
-      <module name="Figure" />
-      <option name="VM_PARAMETERS" />
-      <option name="PROGRAM_PARAMETERS" />
-      <option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
-      <option name="ALTERNATIVE_JRE_PATH" />
-      <option name="PASS_PARENT_ENVS" value="true" />
+    <configuration name="MainKt" type="JetRunConfigurationType" temporary="true" nameIsGenerated="true">
       <option name="MAIN_CLASS_NAME" value="MainKt" />
-      <option name="WORKING_DIRECTORY" />
+      <module name="Figure" />
       <method v="2">
         <option name="Make" enabled="true" />
       </method>

+ 31 - 4
src/Circle.kt

@@ -1,7 +1,34 @@
-// TODO: дополнить определение класса размерами и позицией
-class Circle : Figure(0) {
-    // TODO: реализовать интерфейс Transforming
+class Circle(var posX: Float, var posY: Float, var radius: Float) : Figure(0), Transforming, Movable {
     override fun area(): Float {
-        return  0.toFloat();
+        return Math.PI.toFloat() * radius * radius
+    }
+
+    override fun resize(zoom: Int) {
+        radius *= zoom
+    }
+
+    override fun rotate(direction: RotateDirection, centerX: Int, centerY: Int) {
+        val angle = when (direction) {
+            RotateDirection.Clockwise -> -Math.toRadians(10.0)
+            RotateDirection.CounterClockwise -> Math.toRadians(10.0)
+        }
+
+        // Перемещаем центр вращения в начало координат
+        val translatedX = posX - centerX
+        val translatedY = posY - centerY
+
+        // Поворачиваем точку
+        val rotatedX = translatedX * Math.cos(angle) - translatedY * Math.sin(angle)
+        val rotatedY = translatedX * Math.sin(angle) + translatedY * Math.cos(angle)
+
+        // Перемещаем точку обратно
+        posX = rotatedX.toFloat() + centerX
+        posY = rotatedY.toFloat() + centerY
+    }
+
+
+    override fun move(dx: Int, dy: Int) {
+        posX += dx
+        posY += dy
     }
 }

+ 1 - 2
src/Main.kt

@@ -6,9 +6,8 @@ fun main() {
     movable.move(1,1)
     // переменной базового класса
     val f: Figure = Rect(0,0,1,1)
-    val f2: Figure = Circle()
+    val f2: Figure = Circle(0f, 0f, 10f)
 
     println(f.area())
     println(f2.area())
-
 }

+ 34 - 3
src/Rect.kt

@@ -1,9 +1,9 @@
 // сочетание определения класса и конструктора одновременно объявляет переменные и задаёт их значения
-class Rect(var x: Int, var y: Int, val width: Int, val height: Int) : Movable, Figure(0) {
-    // TODO: реализовать интерфейс Transforming
+class Rect(var x: Int, var y: Int, var width: Int, var height: Int) : Movable, Figure(0), Transforming {
     var color: Int = -1 // при объявлении каждое поле нужно инициализировать
 
     lateinit var name: String // значение на момент определения неизвестно (только для объектных типов)
+
     // дополнительный конструктор вызывает основной
     constructor(rect: Rect) : this(rect.x, rect.y, rect.width, rect.height)
 
@@ -14,6 +14,37 @@ class Rect(var x: Int, var y: Int, val width: Int, val height: Int) : Movable, F
 
     // для каждого класса area() определяется по-своему
     override fun area(): Float {
-        return (width*height).toFloat() // требуется явное приведение к вещественному числу
+        return (width * height).toFloat() // требуется явное приведение к вещественному числу
+    }
+
+    override fun resize(zoom: Int) {
+        width *= zoom; height *= zoom
+    }
+
+    override fun rotate(direction: RotateDirection, centerX: Int, centerY: Int) {
+        val dx = x - centerX
+        val dy = y - centerY
+
+        when (direction) {
+            RotateDirection.Clockwise -> {
+                val newX = centerX + dy
+                val newY = centerY - dx
+                x = newX
+                y = newY
+                val temp = width
+                width = height
+                height = temp
+            }
+
+            RotateDirection.CounterClockwise -> {
+                val newX = centerX - dy
+                val newY = centerY + dx
+                x = newX
+                y = newY
+                val temp = width
+                width = height
+                height = temp
+            }
+        }
     }
 }

+ 31 - 4
src/Square.kt

@@ -1,5 +1,32 @@
-// TODO: дополнить определение класса размерами и позицией
-class Square  {
-    // TODO: унаследовать от Figure, реализовать area()
-    // TODO: реализовать интерфейс Transforming
+class Square(var posX: Float, var posY: Float, var width: Float) : Figure(0), Transforming, Movable {
+    override fun area(): Float {
+        return width * width
+    }
+
+    override fun resize(zoom: Int) {
+        width *= zoom
+    }
+
+    override fun rotate(direction: RotateDirection, centerX: Int, centerY: Int) {
+        val dx = posX - centerX
+        val dy = posY - centerY
+
+        when (direction) {
+            RotateDirection.Clockwise -> {
+                posX = centerX + (-dy)
+                posY = centerY + dx
+            }
+
+            RotateDirection.CounterClockwise -> {
+                posX = centerX + dy
+                posY = centerY - dx
+            }
+        }
+    }
+
+
+    override fun move(dx: Int, dy: Int) {
+        posX += dx
+        posY += dy
+    }
 }

+ 0 - 2
src/Transforming.kt

@@ -1,9 +1,7 @@
 interface Transforming {
     fun resize(zoom: Int)
-    // TODO: величивает фигуру, не перемещая, с сохранением пропорций
 
     fun rotate(direction: RotateDirection, centerX: Int, centerY: Int)
-    // TODO: поворот фигуры вокруг точки (centerX, centerY) на 90 градусов
 }
 
 enum class RotateDirection {