defining a procedure using graphics-draw-line - graphics

Can you see what is wrong with this:
(define (box d x1 y1 x2 y2) (
(graphics-draw-line d x1 y1 x1 y2)
(graphics-draw-line d x1 y2 x2 y2)
(graphics-draw-line d x2 y2 x2 y1)
(graphics-draw-line d x2 y1 x1 y1) ))
When I call it like this:
( begin
(define w (make-graphics-device 'x))
(box w .10 .10 .20 .20) )
I get an errer:
;The object #!unspecific is not applicable.
;To continue, call RESTART with an option number:
; (RESTART 2) => Specify a procedure to use in its place.
; (RESTART 1) => Return to read-eval-print level 1.
2 error>
This works:
(begin
(define w (make-graphics-device 'x))
(graphics-draw-line w .1 .1 .1 .2)
(graphics-draw-line w .1 .2 .2 .2)
(graphics-draw-line w .2 .2 .2 .1)
(graphics-draw-line w .2 .1 .1 .1) )
I can't see the difference!

Don't just group expressions with ()s -- that will try to use the result of the first as a function, but the value is #!unspecific -- definitely not a function.
Use this:
(define (box d x1 y1 x2 y2)
(graphics-draw-line d x1 y1 x1 y2)
(graphics-draw-line d x1 y2 x2 y2)
(graphics-draw-line d x2 y2 x2 y1)
(graphics-draw-line d x2 y1 x1 y1))

Related

Why the 2D spline graph is incorrect?

I started getting into spline principles to use them in my studies. However, I got stuck in programming the code for Wikipedia [example] . The final graph doesn't correspond to the Wiki's example. I get two curves that are not connected, despite the introduction of the parameter t(x):
Spline segments
Would you mind helping me in finding the cause of the error? Thanks.
import numpy as np
import matplotlib.pyplot as plt
x0, y0, x1, y1, x2, y2 = -1, 0.5, 0, 0, 3, 3
def spline(x0, y0, x1, y1, x2, y2):
a11 = 2 / (x1 - x0)
a12 = 1 / (x1 - x0)
a13 = 0
a21 = a12
a22 = 2 * (1 / (x1 - x0) + 1 / (x2 - x1))
a23 = 1 / (x2 - x1)
a31 = a13
a32 = a23
a33 = 2 / (x2 - x1)
b1 = 3 * ((y1 - y0) / (x1 - x0) ** 2)
b2 = 3 * ((y1 - y0) / (x1 - x0) ** 2 + (y2 - y1) / (x2 - x1) ** 2)
b3 = 3 * ((y2 - y1) / (x2 - x1) ** 2)
A = np.array([[a11, a12, a13], [a21, a22, a23], [a31, a32, a33]])
L = np.array([[-b1], [-b2], [-b3]])
AT = np.transpose(A)
ATA = np.matmul(AT, A)
invATA = np.linalg.inv(ATA)
ATL = np.matmul(AT, L)
X = - np.matmul(invATA, ATL)
[k0, k1, k2] = X
a1 = k0 * (x1 - x0) - (y1 - y0)
b1 = -k1 * (x1 - x0) + (y1 - y0)
a2 = k1 * (x2 - x1) - (y2 - y1)
b2 = -k2 * (x2 - x1) + (y2 - y1)
return a1[0], b1[0], a2[0], b2[0]
a1, b1, a2, b2 = spline(-1, 0.5, 0, 0, 3, 3)
def q1(t):
return (1 - t) * y0 + t * y1 + t *(1 - t) * ((1 - t) * a1 + t * b1)
def q2(t):
return (1 - t) * y1 + t * y2 + t *(1 - t) * ((1 - t) * a2 + t * b2)
x_list = []
y1_list = []
y2_list = []
for x in range(1000):
t = (x/1000 - x1) / (x2 - x1)
x_list.append(x)
y1_list.append(q1(t))
y2_list.append(q2(t))
plt.scatter(x_list, y1_list)
plt.scatter(x_list, y2_list)
I found it! The key was the proper x-range corresponding to the 3 given points.
import numpy as np
import matplotlib.pyplot as plt
x0, y0, x1, y1, x2, y2 = -1, 0.5, 0, 0, 3, 3
def spline(x0, y0, x1, y1, x2, y2):
a11 = 2 / (x1 - x0)
a12 = 1 / (x1 - x0)
a13 = 0
a21 = a12
a22 = 2 * (1 / (x1 - x0) + 1 / (x2 - x1))
a23 = 1 / (x2 - x1)
a31 = a13
a32 = a23
a33 = 2 / (x2 - x1)
b1 = 3 * ((y1 - y0) / (x1 - x0) ** 2)
b2 = 3 * ((y1 - y0) / (x1 - x0) ** 2 + (y2 - y1) / (x2 - x1) ** 2)
b3 = 3 * ((y2 - y1) / (x2 - x1) ** 2)
A = np.array([[a11, a12, a13], [a21, a22, a23], [a31, a32, a33]])
L = np.array([[-b1], [-b2], [-b3]])
AT = np.transpose(A)
ATA = np.matmul(AT, A)
invATA = np.linalg.inv(ATA)
ATL = np.matmul(AT, L)
X = - np.matmul(invATA, ATL)
[k0, k1, k2] = X
a1 = k0 * (x1 - x0) - (y1 - y0)
b1 = -k1 * (x1 - x0) + (y1 - y0)
a2 = k1 * (x2 - x1) - (y2 - y1)
b2 = -k2 * (x2 - x1) + (y2 - y1)
return a1[0], b1[0], a2[0], b2[0]
a1, b1, a2, b2 = spline(-1, 0.5, 0, 0, 3, 3)
def q1(t):
return (1 - t) * y0 + t * y1 + t *(1 - t) * ((1 - t) * a1 + t * b1)
def q2(t):
return (1 - t) * y1 + t * y2 + t *(1 - t) * ((1 - t) * a2 + t * b2)
x1_list = []
x2_list = []
y1_list = []
y2_list = []
for x in range(-1000, 0, 1):
t = (x/1000 - x0) / (x1 - x0)
x1_list.append(x)
y1_list.append(q1(t))
for x in range(3000):
t = (x/1000 - x1) / (x2 - x1)
x2_list.append(x)
y2_list.append(q2(t))
plt.scatter(x1_list, y1_list)
plt.scatter(x2_list, y2_list)

Allocating values in Haskell

Im very new to haskell and I have to define the elliptic curve functions from the RFC6090 Definition in Haskell. (RFC6090: https://www.rfc-editor.org/rfc/rfc6090#section-3)
For that I have to implement a function called ecAdd.
ecAdd returns values from the type ECPoint. I defined them but Im not sure why the values x1 and x2 are not recognized. I get the following error for every section where x1,x2,y1 or y2 appears.
error:
• Couldn't match expected type ‘(Double -> Double) -> Fq -> Fq’
with actual type ‘galois-field-1.0.0:Data.Field.Galois.Prime.Prime
Q’
• The function ‘y2 - y1’ is applied to two arguments,
but its type ‘galois-field-1.0.0:Data.Field.Galois.Prime.Prime Q’
has none
In the first argument of ‘(^)’, namely
‘((y2 - y1) recip (x2 - x1))’
In the first argument of ‘(-)’, namely
‘((y2 - y1) recip (x2 - x1)) ^ 2’
|
64 | x3 = ((y2 - y1) recip (x2 - x1))^2 - x1 - x2
Here is my function code
ecAdd
:: ECPoint -- ^ Punkt P
-> ECPoint -- ^ Punkt Q
-> ECPoint -- ^ Ergebnis P + Q
ecAdd q O = q
ecAdd O p = p
ecAdd (A x1 y1) (A x2 y2) =
if (A x1 y1) /= (A x2 y2)
then
if x1 == x2
then O
else let x3 = ((y2-y1) recip (x2-x1))^2 - x1 - x2
y3 = (x1-x3)*(y2-y1) recip (x2-x1) - y1 in A x3 y3
else
if y1 == 0
then O
else let x4 = ((3*x1^2 + P256K1._a) recip (2*y1))^2 - 2*x1
y4 = (x1-x3)*(3*x1^2 + P256K1._a) recip (2*y1) - y1
in A x4 y4

Rotate array of points according one point

I have 4 points and angle (as shown on the picture). How to get new point values for rotated object?
picture(x/y axis inverted, mistake. Vertical should be Y, horizontal - X)
At first, get coordinates relative to the rotation origin (x0, y0)
x' = x1 - x0
y' = y1 - y0
Then rotate
x'' = x' * Cos(Fi) - y' * Sin(Fi)
y'' = x' * Sin(Fi) + y' * Cos(Fi)
And now shift coordinates back
x_r = x'' + x0
y_r = y'' + y0

How to find an x y coordinate that is d distance away from a given point on slope m?

We are trying to define a Haskell function that gives the coordinates x, y given a starting point (x0, y0), distance from the starting coord, and slope of the line that goes through both points. Any ideas?
It will probably have a signature of
endPoint :: (Double, Double) -> double -> double -> (Double, Double)
How you would do this on paper is to use the distance formula (with the second point as variables/unknowns), which then gives you some ratio of the x and y for this new point, use that ratio as the substitution into the point-slope formula, which should give one of y and x, then plug that value into the formula again to get the x and y we don't have yet.
From this question, we can do the distance formula, but how do we deal with substitution? Is that possible in Haskell?
According to the definition of slope, this problem could be solved like this:
endPoint :: (Double, Double) -> Double -> Double -> (Double, Double)
endPoint (x0, y0) m d = (x0 + dx, y0 + dy)
where theta = atan m
dx = d * cos theta
dy = d * sin theta
Here is an example about point (2,8) and (3,20):
*Main> endPoint (2,8) 12 (sqrt $ 12 * 12 + 1)
(3.000000000000001,20.0)

calculate distance between two points (Haskell)

Given an input of two tupples, i want to be able to calculate the distance between two points using the formula:
distance = sqrt ( (x1 - x2) ^ 2 + (y1 - y2) ^2 )
so i want the function call and output to look like this:
-- > distance (5 , 10) (3 , 5)
-- 5.385...
when i try to run my code below, it tells me parse error on input 'where'. Can anyone help me resolve my issue? Here is my code:
distance (x1 , y1) (x2 , y2) = sqrt (x'*x' + y'*y')
where
x' = x1 - x2
y' = y1 - y2
You are making an indendation error, this should work- see how where clause is indented:
distance (x1 , y1) (x2 , y2) = sqrt (x'*x' + y'*y')
where
x' = x1 - x2
y' = y1 - y2

Resources