Skip to content

Instantly share code, notes, and snippets.

@glorphindale
Created July 7, 2014 16:29
Show Gist options
  • Save glorphindale/1b97f91d101e888c1541 to your computer and use it in GitHub Desktop.
Save glorphindale/1b97f91d101e888c1541 to your computer and use it in GitHub Desktop.
Коэффициенты косинуса и синуса
(ns visimath.circles
(:import [java.awt.event KeyEvent])
(:require [quil.core :as qc]))
(def state (atom {:sin 1 :cos 1}))
(defn deg->pos [radius deg cos-k sin-k]
(let [rads (qc/radians (* 1 deg))
posx (* radius (Math/cos (* cos-k rads)))
posy (* radius (Math/sin (* sin-k rads)))]
[posx posy]))
(def psize 10)
(defn draw []
(let [ts (rem (qc/frame-count) 360)
cos-k (@state :cos)
sin-k (@state :sin)
center-x (/ (qc/width) 2)
center-y (/ (qc/height) 2)
[posx posy] (deg->pos 100 ts cos-k sin-k)]
(qc/with-fill [0 10]
(qc/rect 0 15 (qc/width) 35)
(qc/rect 15 0 35 (qc/height)))
(qc/with-fill [255 25]
(qc/with-translation [center-x center-y]
(qc/ellipse posx posy psize psize))
(qc/with-translation [center-x 20]
(qc/ellipse posx 20 psize psize))
(qc/with-translation [20 center-y]
(qc/ellipse 20 posy psize psize)))
))
(defn setup []
(qc/smooth)
(qc/no-stroke)
(qc/background 0)
(qc/frame-rate 60)
(swap! state (fn [_] {:cos 1 :sin 1})))
(defn key-pressed []
(qc/background 0)
(cond
(= (qc/key-code) KeyEvent/VK_DOWN)
(swap! state update-in [:cos] dec)
(= (qc/key-code) KeyEvent/VK_UP)
(swap! state update-in [:cos] inc)
(= (qc/key-code) KeyEvent/VK_LEFT)
(swap! state update-in [:sin] dec)
(= (qc/key-code) KeyEvent/VK_RIGHT)
(swap! state update-in [:sin] inc)))
(qc/defsketch circles
:title "Circles"
:size [400 400]
:draw draw
:setup setup
:key-pressed key-pressed
:renderer :opengl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment