1/*
2 Project: Graphos
3 GRFunctions.m
4
5 Utility functions.
6
7 Copyright (C) 2000-2013 GNUstep Application Project
8
9 Author: Enrico Sersale (original GDraw implementation)
10 Author: Ing. Riccardo Mottola
11
12 This application is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public
14 License as published by the Free Software Foundation; either
15 version 2 of the License, or (at your option) any later version.
16
17 This application is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 Library General Public License for more details.
21
22 You should have received a copy of the GNU General Public
23 License along with this library; if not, write to the Free
24 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
25 */
26
27#import "GRFunctions.h"
28
29#include <math.h>
30#include <unistd.h>
31
32#define GD_PI 3.14159265358979323846
33
34NSPoint pointApplyingCostrainerToPoint(NSPoint p, NSPoint sp)
35{
36    float cos22 = cos(GD_PI * 22 / 180);
37    float cos45 = cos(GD_PI * 45 / 180);
38    float cos67 = cos(GD_PI * 67 / 180);
39    double cy22, cy45, cy67, diffx, diffy;
40    NSPoint cp;
41
42    diffx = grmax(p.x, sp.x) - grmin(p.x, sp.x);
43    diffy = grmax(p.y, sp.y) - grmin(p.y, sp.y);
44
45    cy22 = diffx * pow(1 - pow(cos22, 2), 0.5) / cos22;
46    cy45 = diffx * pow(1 - pow(cos45, 2), 0.5) / cos45;
47    cy67 = diffx * pow(1 - pow(cos67, 2), 0.5) / cos67;
48
49    if(diffy < cy45) {
50        cp.x = p.x;
51        if(diffy > cy22 && diffy < cy67)
52        {
53            if(p.y > sp.y)
54                cp.y = sp.y + cy45;
55            else
56                cp.y = sp.y - cy45;
57        } else
58        {
59            cp.y = sp.y;
60        }
61    } else {
62        cp.x = sp.x;
63        cp.y = p.y;
64    }
65
66    return cp;
67}
68
69NSRect GRMakeBounds(float x, float y, float width, float height)
70{
71    if (width < 0)
72    {
73        width = -width;
74        x = x - width;
75    }
76    if (height < 0)
77    {
78        height = -height;
79        y = y - height;
80    }
81    return NSMakeRect(x, y, width, height);
82}
83
84NSPoint GRpointDeZoom(NSPoint p, float zf)
85{
86  return NSMakePoint(p.x / zf, p.y / zf);
87}
88
89NSPoint GRpointZoom(NSPoint p, float zf)
90{
91  return NSMakePoint(p.x * zf, p.y * zf);
92}
93
94BOOL pointInRect(NSRect rect, NSPoint p)
95{
96    if(p.x >= rect.origin.x
97       && p.x <= (rect.origin.x + rect.size.width)
98       && p.y >= rect.origin.y
99       && p.y <= (rect.origin.y + rect.size.height))
100        return YES;
101
102    return NO;
103}
104
105double grmin(double a, double b) {
106    if(a < b)
107        return a;
108    else
109        return b;
110}
111
112double grmax(double a, double b) {
113    if(a > b)
114        return a;
115    else
116        return b;
117}
118
119
120