diff --git a/.idea/misc.xml b/.idea/misc.xml index 8ab2039..e7f70de 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,15 +3,22 @@ diff --git a/app/build.gradle b/app/build.gradle index ef83eef..225cc27 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -4,7 +4,6 @@ plugins { id 'kotlin-android-extensions' id 'kotlin-kapt' id 'org.jetbrains.kotlin.android' - //id 'android.navigation.safeargs.kotlin' id 'androidx.navigation.safeargs' } @@ -49,11 +48,10 @@ android { dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) - implementation 'androidx.core:core-ktx:1.7.0' - //implementation "org.jetbrians.kotlin:kotlin-stdlib:$kotlin_version" - implementation 'androidx.appcompat:appcompat:1.4.1' - implementation 'com.google.android.material:material:1.5.0' - implementation 'androidx.constraintlayout:constraintlayout:2.1.3' + implementation 'androidx.core:core-ktx:1.8.0' + implementation 'androidx.appcompat:appcompat:1.4.2' + implementation 'com.google.android.material:material:1.6.1' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.legacy:legacy-support-v4:1.0.0' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' diff --git a/app/src/main/elektromagnet-playstore.png b/app/src/main/elektromagnet-playstore.png new file mode 100644 index 0000000..115bf1c Binary files /dev/null and b/app/src/main/elektromagnet-playstore.png differ diff --git a/app/src/main/java/com/example/acapendulum20/canvas/CanvasPendulum.kt b/app/src/main/java/com/example/acapendulum20/canvas/CanvasPendulum.kt index 2115a21..06781e8 100644 --- a/app/src/main/java/com/example/acapendulum20/canvas/CanvasPendulum.kt +++ b/app/src/main/java/com/example/acapendulum20/canvas/CanvasPendulum.kt @@ -1,78 +1,130 @@ package com.example.acapendulum20.canvas +import android.R import android.content.Context +import android.content.res.Resources +import android.graphics.BitmapFactory import android.graphics.Canvas import android.graphics.Color import android.graphics.Paint import android.util.AttributeSet import android.view.View +import android.widget.ImageView +import android.widget.LinearLayout +import kotlin.math.PI +import kotlin.math.cos +import kotlin.math.sin + class CanvasPendulum @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr) { - private val paint: Paint = Paint() private lateinit var canvas: Canvas - private var pendulumLineStopX = 200f - private var pendulumLineStopY = 600f - //var pendulumOnMove = false - - - + private val mPendulum: Pendulum = Pendulum() override fun onDraw(canvas: Canvas?) { super.onDraw(canvas) - paint.style = Paint.Style.STROKE - //paint.strokeWidth = 10f - //canvas?.drawCircle(500f, 700f, 70f, paint) - paint.strokeWidth = 9f - canvas?.drawLine(455f,0f,500f, 630f,paint) - //if canvas not NULL then this canvas = it canvas?.let { this.canvas = it } - } - fun startPendulum() { - //pendulumOnMove = false + //println("ONDRAW") + //draw line + mPendulum.startPosition.x = (width/2).toFloat() + paint.isAntiAlias = true paint.style = Paint.Style.STROKE - //paint.strokeWidth = 10f - //canvas.drawCircle(200f, 700f, 70f, paint)// cx&cy -> Variable - paint.strokeWidth = 9f - canvas.drawColor(Color.WHITE) - canvas.drawLine(455f,0f,pendulumLineStopX, pendulumLineStopY,paint) //stopX&stopY -> Variable - println("excess to startPendulum function") - } + paint.color = Color.RED + paint.strokeWidth = 10f + this.canvas.drawLine(mPendulum.startPosition.x, mPendulum.startPosition.y, mPendulum.stopPosition.x, mPendulum.stopPosition.y, paint) + + //draw circle + paint.style = Paint.Style.FILL_AND_STROKE + paint.strokeWidth = 1f + paint.color = Color.GRAY + canvas?.drawCircle(mPendulum.stopPosition.x, mPendulum.stopPosition.y, mPendulum.ballr, paint) + + //draw magnet as black circle + paint.style = Paint.Style.FILL_AND_STROKE + paint.strokeWidth = 1f + paint.color = Color.DKGRAY + canvas?.drawCircle((width/2).toFloat(),1150f, 30f, paint) + + //draw sensor left + + paint.style = Paint.Style.FILL_AND_STROKE + paint.strokeWidth = 1f + paint.color = Color.RED + if (mPendulum.angleVelocity > 0 ){ + if (mPendulum.stopPosition.x < (width/3) - 30f && mPendulum.stopPosition.x +30 > (width/3) - 30f - mPendulum.ballr){ + paint.style = Paint.Style.FILL_AND_STROKE + paint.strokeWidth = 10f + paint.color = Color.GREEN + } + } + canvas?.drawCircle((width/3).toFloat(),910f, 30f, paint) + + //draw sensor right + + paint.style = Paint.Style.FILL_AND_STROKE + paint.strokeWidth = 10f + paint.color = Color.RED + if (mPendulum.angleVelocity < 0 ){ + if (mPendulum.stopPosition.x > (width/1.5) - 30f && mPendulum.stopPosition.x +30 < (width/1.5) + 30f + mPendulum.ballr){ + paint.style = Paint.Style.FILL_AND_STROKE + paint.strokeWidth = 10f + paint.color = Color.GREEN + } + } + canvas?.drawCircle((width/1.5).toFloat(),910f, 30f, paint) - fun stopDrawingPendulum() { - //canvas.drawCircle(400f, 300f, 70f, paint) - //pendulumOnMove = false - //canvas.drawColor(Color.WHITE) - //canvas.drawLine(455f,0f,500f, 630f,paint) //stopX&stopY -> Variable - println("excess to stopDrawingPendulum function") + startPendulum() + invalidate() } -} - - - - + fun startPendulum() { + val gravity = 0.981f + val damping = 0.996f + + mPendulum.angleAcceleration = ((-1 * gravity / mPendulum.r) * (mPendulum.angle)).toFloat() + mPendulum.angleVelocity += mPendulum.angleAcceleration + mPendulum.angleVelocity *= damping + mPendulum.angle += mPendulum.angleVelocity + //Polar to cartesian conversion + mPendulum.stopPosition.x = mPendulum.r * (sin(mPendulum.angle)).toFloat() + mPendulum.stopPosition.y = mPendulum.r * (cos(mPendulum.angle)).toFloat() + //Origin transition + mPendulum.stopPosition.x += mPendulum.startPosition.x + } + fun startMagnet(){ + //if Circle = sensor -> circle/angleVelocity ++ + if (mPendulum.stopPosition.x == canvas.width/2f) { + mPendulum.angleVelocity = 0f + } + } + fun stopMagnet(){ + } +} +class Position(var x: Float, var y: Float){ +} -/**/ \ No newline at end of file +class Pendulum { + + var startPosition: Position = Position(0f, 0f) + var stopPosition: Position = Position(0f,0f) + var r = 800f + //PI/6 Max ausschwingen + var angle : Double = Math.PI/6 + var angleVelocity : Float = 0.0f // + var angleAcceleration : Float = 0.0f // + var ballr : Float = 70.0f // +} \ No newline at end of file diff --git a/app/src/main/java/com/example/acapendulum20/fragments/pendulum/PendulumFragment.kt b/app/src/main/java/com/example/acapendulum20/fragments/pendulum/PendulumFragment.kt index 39bc833..54bbb75 100644 --- a/app/src/main/java/com/example/acapendulum20/fragments/pendulum/PendulumFragment.kt +++ b/app/src/main/java/com/example/acapendulum20/fragments/pendulum/PendulumFragment.kt @@ -2,6 +2,7 @@ package com.example.acapendulum20.fragments.pendulum import android.annotation.SuppressLint import android.os.Bundle +import android.os.SystemClock import android.view.LayoutInflater import android.view.MotionEvent import android.view.View @@ -10,6 +11,7 @@ import android.widget.Button import androidx.fragment.app.Fragment import com.example.acapendulum20.R import com.example.acapendulum20.canvas.CanvasPendulum +import java.util.* class PendulumFragment : Fragment() { @@ -32,27 +34,28 @@ class PendulumFragment : Fragment() { //} - button.setOnTouchListener { _, event -> - when (event.action) { - - MotionEvent.ACTION_DOWN -> { - println("button gedrückt") - mCanvasPendulum.startPendulum() + button.setOnTouchListener(object : View.OnTouchListener{ + var pushed = false + override fun onTouch(v: View?, event: MotionEvent?): Boolean { + var steps: Int + var startTime: Long + if (event?.action == MotionEvent.ACTION_DOWN){ + startTime = SystemClock.elapsedRealtime() + pushed = true + do { + mCanvasPendulum.startMagnet() + steps =- 1 + }while (pushed) } - //MotionEvent.ACTION_MOVE -> {} - - MotionEvent.ACTION_UP -> { - println("button losgelassen") - mCanvasPendulum.stopDrawingPendulum() + if (event?.action == MotionEvent.ACTION_UP){ + pushed = false + mCanvasPendulum.stopMagnet() + println("button action up") } - - //MotionEvent.ACTION_CANCEL -> { } - - //else -> {} + return true } - true - } + }) //System.currentTimeMillis() @@ -60,6 +63,4 @@ class PendulumFragment : Fragment() { return view } -} - - +} \ No newline at end of file diff --git a/app/src/main/res/drawable-xxhdpi/digitalsensor.png b/app/src/main/res/drawable-xxhdpi/digitalsensor.png new file mode 100644 index 0000000..8c7a2d7 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/digitalsensor.png differ diff --git a/app/src/main/res/drawable-xxhdpi/elektromagnet.png b/app/src/main/res/drawable-xxhdpi/elektromagnet.png new file mode 100644 index 0000000..13b5134 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/elektromagnet.png differ diff --git a/app/src/main/res/layout/fragment_pendulum.xml b/app/src/main/res/layout/fragment_pendulum.xml index a6e585c..9a93195 100644 --- a/app/src/main/res/layout/fragment_pendulum.xml +++ b/app/src/main/res/layout/fragment_pendulum.xml @@ -5,67 +5,66 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - android:padding="24dp" + android:padding="1dp" tools:context=".fragments.pendulum.PendulumFragment"> + app:layout_constraintStart_toEndOf="@+id/textView6" /> + app:layout_constraintBottom_toTopOf="@+id/textView4" + app:layout_constraintStart_toStartOf="parent" /> + app:layout_constraintStart_toEndOf="@+id/textView4" /> - - - @@ -74,18 +73,31 @@ android:id="@+id/textView11" android:layout_width="wrap_content" android:layout_height="wrap_content" + android:layout_marginStart="8dp" + android:layout_marginBottom="20dp" android:text="TextView" app:layout_constraintBottom_toTopOf="@+id/textView7" app:layout_constraintStart_toEndOf="@+id/textView10" /> + +