Skip to content

Commit

Permalink
ch7 updates
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenhalim committed May 12, 2020
1 parent e2373cc commit ac005f9
Show file tree
Hide file tree
Showing 6 changed files with 431 additions and 305 deletions.
67 changes: 35 additions & 32 deletions ch7/circles.cpp
Original file line number Diff line number Diff line change
@@ -1,65 +1,68 @@
#include <bits/stdc++.h>
using namespace std;

#define INF 1e9
#define EPS 1e-9
#define PI acos(-1.0)
double DEG_to_RAD(double d) { return d*M_PI/180.0; }

double DEG_to_RAD(double d) { return d * PI / 180.0; }
double RAD_to_DEG(double r) { return r*180.0/M_PI; }

double RAD_to_DEG(double r) { return r * 180.0 / PI; }
struct point_i {
int x, y; // use this if possible
point_i() { x = y = 0; } // default constructor
point_i(int _x, int _y) : x(_x), y(_y) {} // constructor
};

struct point_i { int x, y; // whenever possible, work with point_i
point_i() { x = y = 0; } // default constructor
point_i(int _x, int _y) : x(_x), y(_y) {} }; // constructor
struct point {
double x, y; // if need more precision
point() { x = y = 0.0; } // default constructor
point(double _x, double _y) : x(_x), y(_y) {} // constructor
};

struct point { double x, y; // only used if more precision is needed
point() { x = y = 0.0; } // default constructor
point(double _x, double _y) : x(_x), y(_y) {} }; // constructor

int insideCircle(point_i p, point_i c, int r) { // all integer version
int dx = p.x - c.x, dy = p.y - c.y;
int Euc = dx * dx + dy * dy, rSq = r * r; // all integer
return Euc < rSq ? 0 : Euc == rSq ? 1 : 2; } //inside/border/outside
int insideCircle(point_i p, point_i c, int r) { // all integer version
int dx = p.x-c.x, dy = p.y-c.y;
int Euc = dx*dx + dy*dy, rSq = r*r; // all integer
return Euc < rSq ? 1 : Euc == rSq ? 0 : -1; // inside/border/outside
}

bool circle2PtsRad(point p1, point p2, double r, point &c) {
double d2 = (p1.x - p2.x) * (p1.x - p2.x) +
(p1.y - p2.y) * (p1.y - p2.y);
double det = r * r / d2 - 0.25;
// to get the other center, reverse p1 and p2
double d2 = (p1.x-p2.x) * (p1.x-p2.x) +
(p1.y-p2.y) * (p1.y-p2.y);
double det = r*r / d2 - 0.25;
if (det < 0.0) return false;
double h = sqrt(det);
c.x = (p1.x + p2.x) * 0.5 + (p1.y - p2.y) * h;
c.y = (p1.y + p2.y) * 0.5 + (p2.x - p1.x) * h;
return true; } // to get the other center, reverse p1 and p2
c.x = (p1.x+p2.x) * 0.5 + (p1.y-p2.y) * h;
c.y = (p1.y+p2.y) * 0.5 + (p2.x-p1.x) * h;
return true;
}

int main() {
// circle equation, inside, border, outside
point_i pt(2, 2);
int r = 7;
point_i inside(8, 2);
printf("%d\n", insideCircle(inside, pt, r)); // 0-inside
printf("%d\n", insideCircle(inside, pt, r)); // 1, inside
point_i border(9, 2);
printf("%d\n", insideCircle(border, pt, r)); // 1-at border
printf("%d\n", insideCircle(border, pt, r)); // 0, at border
point_i outside(10, 2);
printf("%d\n", insideCircle(outside, pt, r)); // 2-outside
printf("%d\n", insideCircle(outside, pt, r)); // -1, outside

double d = 2 * r;
double d = 2*r;
printf("Diameter = %.2lf\n", d);
double c = PI * d;
double c = M_PI*d;
printf("Circumference (Perimeter) = %.2lf\n", c);
double A = PI * r * r;
double A = M_PI*r*r;
printf("Area of circle = %.2lf\n", A);

printf("Length of arc (central angle = 60 degrees) = %.2lf\n", 60.0 / 360.0 * c);
printf("Length of chord (central angle = 60 degrees) = %.2lf\n", sqrt((2 * r * r) * (1 - cos(DEG_to_RAD(60.0)))));
printf("Area of sector (central angle = 60 degrees) = %.2lf\n", 60.0 / 360.0 * A);
printf("Length of arc (central angle = 60 degrees) = %.2lf\n", 60.0/360.0 * c);
printf("Length of chord (central angle = 60 degrees) = %.2lf\n", sqrt((2*r*r) * (1 - cos(DEG_to_RAD(60.0)))));
printf("Area of sector (central angle = 60 degrees) = %.2lf\n", 60.0/360.0 * A);

point p1;
point p2(0.0, -1.0);
point ans;
circle2PtsRad(p1, p2, 2.0, ans);
printf("One of the center is (%.2lf, %.2lf)\n", ans.x, ans.y);
circle2PtsRad(p2, p1, 2.0, ans); // we simply reverse p1 with p2
circle2PtsRad(p2, p1, 2.0, ans); // reverse p1 with p2
printf("The other center is (%.2lf, %.2lf)\n", ans.x, ans.y);

return 0;
Expand Down
52 changes: 27 additions & 25 deletions ch7/circles.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
class ch7_02_circles {
double DEG_to_RAD(double d) { return d * Math.PI / 180.0; }
double RAD_to_DEG(double r) { return r * 180.0 / Math.PI; }
class circles {
double DEG_to_RAD(double d) { return d*Math.PI/180.0; }
double RAD_to_DEG(double r) { return r*180.0/Math.PI; }

class point_i {
int x, y; // whenever possible, work with point_i
point_i() { x = y = 0; } // default constructor
point_i(int _x, int _y) { x = _x; y = _y; } // constructor
int x, y; // use this if possible
point_i() { x = y = 0; } // default constructor
point_i(int _x, int _y) { x = _x; y = _y; } // constructor
};

class point {
double x, y; // only used if more precision is needed
point() { x = y = 0.0; } // default constructor
point(double _x, double _y) { x = _x; y = _y; } // constructor
double x, y; // if need more precision
point() { x = y = 0.0; } // default constructor
point(double _x, double _y) { x = _x; y = _y; } // constructor
};

int insideCircle(point_i p, point_i c, int r) { // all integer version
int dx = p.x - c.x, dy = p.y - c.y;
int Euc = dx * dx + dy * dy, rSq = r * r; // all integer
return Euc < rSq ? 0 : Euc == rSq ? 1 : 2; } // inside/border/outside
int dx = p.x-c.x, dy = p.y-c.y;
int Euc = dx*dx + dy*dy, rSq = r*r; // all integer
return Euc < rSq ? 1 : Euc == rSq ? 0 : -1; // inside/border/outside
}

boolean circle2PtsRad(point p1, point p2, double r, point c) {
double d2 = (p1.x - p2.x) * (p1.x - p2.x) +
(p1.y - p2.y) * (p1.y - p2.y);
double det = r * r / d2 - 0.25;
double d2 = (p1.x-p2.x) * (p1.x-p2.x) +
(p1.y-p2.y) * (p1.y-p2.y);
double det = r*r / d2 - 0.25;
if (det < 0.0) return false;
double h = Math.sqrt(det);
c.x = (p1.x + p2.x) * 0.5 + (p1.y - p2.y) * h;
c.y = (p1.y + p2.y) * 0.5 + (p2.x - p1.x) * h;
return true; }
c.x = (p1.x+p2.x) * 0.5 + (p1.y-p2.y) * h;
c.y = (p1.y+p2.y) * 0.5 + (p2.x-p1.x) * h;
return true;
}

void run() {
// circle equation, inside, border, outside
point_i pt = new point_i(2, 2);
int r = 7;
point_i inside = new point_i(8, 2);
System.out.printf("%d\n", insideCircle(inside, pt, r)); // 0-inside
System.out.printf("%d\n", insideCircle(inside, pt, r)); // 1, inside
point_i border = new point_i(9, 2);
System.out.printf("%d\n", insideCircle(border, pt, r)); // 1-at border
System.out.printf("%d\n", insideCircle(border, pt, r)); // 0, at border
point_i outside = new point_i(10, 2);
System.out.printf("%d\n", insideCircle(outside, pt, r)); // 2-outside
System.out.printf("%d\n", insideCircle(outside, pt, r)); // -1, outside

double d = 2 * r;
System.out.printf("Diameter = %.2f\n", d);
Expand All @@ -47,10 +49,10 @@ void run() {
double A = Math.PI * r * r;
System.out.printf("Area of circle = %.2f\n", A);

System.out.printf("Length of arc (central angle = 60 degrees) = %.2f\n", 60.0 / 360.0 * c);
System.out.printf("Length of arc (central angle = 60 degrees) = %.2f\n", 60.0/360.0*c);
System.out.printf("Length of chord (central angle = 60 degrees) = %.2f\n",
Math.sqrt((2 * r * r) * (1 - Math.cos(DEG_to_RAD(60.0)))));
System.out.printf("Area of sector (central angle = 60 degrees) = %.2f\n", 60.0 / 360.0 * A);
Math.sqrt((2*r*r) * (1 - Math.cos(DEG_to_RAD(60.0)))));
System.out.printf("Area of sector (central angle = 60 degrees) = %.2f\n", 60.0/360.0*A);

point p1 = new point();
point p2 = new point(0.0, -1.0);
Expand All @@ -62,6 +64,6 @@ void run() {
}

public static void main(String[] args){
new ch7_02_circles().run();
new circles().run();
}
}
64 changes: 64 additions & 0 deletions ch7/circles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import math

def DEG_to_RAD(d): return d*math.pi/180.0

def RAD_to_DEG(r): return r*180.0/math.pi

class point:
def __init__(self, _x, _y): # int or double
self.x = _x
self.y = _y
def getX(self):
return self.x
def getY(self):
return self.y

# returns 0/1/2 for inside/border/outside, respectively
def insideCircle(p, c, r): # all integer version
dx = p.getX()-c.getX()
dy = p.getY()-c.getY()
Euc = dx*dx + dy*dy
rSq = r*r
return 1 if Euc < rSq else (0 if Euc == rSq else -1)

def circle2PtsRad(p1, p2, r, c_list):
# to get the other center, reverse p1 and p2
d2 = (p1.getX()-p2.getX()) * (p1.getX()-p2.getX()) + (p1.getY()-p2.getY()) * (p1.getY()-p2.getY())
det = r*r / d2 - 0.25
if det < 0.0: return False
h = math.sqrt(det)
c_list[0].x = (p1.getX()+p2.getX()) * 0.5 + (p1.getY()-p2.getY()) * h
c_list[0].y = (p1.getY()+p2.getY()) * 0.5 + (p2.getX()-p1.getX()) * h
return True

def main():
# circle equation, inside, border, outside
pt = point(2, 2)
r = 7
inside = point(8, 2)
print(insideCircle(inside, pt, r)) # 1, inside
border = point (9, 2)
print(insideCircle(border, pt, r)) # 0, at border
outside = point(10, 2)
print(insideCircle(outside, pt, r)) # -1, outside

d = 2*r
print("Diameter = ", "{:.2f}".format(d))
c = math.pi*d
print("Circumference (Perimeter) = ", "{:.2f}".format(c))
A = math.pi*r*r
print("Area of circle = ", "{:.2f}".format(A))

print("Length of arc (central angle = 60 degrees) = ", "{:.2f}".format(60.0/360.0 * c))
print("Length of chord (central angle = 60 degrees) = ", "{:.2f}".format(math.sqrt((2*r*r) * (1 - math.cos(DEG_to_RAD(60.0))))))
print("Area of sector (central angle = 60 degrees) = ", "{:.2f}".format(60.0/360.0 * A))

p1 = point(0, 0)
p2 = point(0.0, -1.0)
ans = [point(0, 0)] # use a wrapper
circle2PtsRad(p1, p2, 2.0, ans)
print("One of the center is (", "{:.2f}".format(ans[0].getX()), ",", "{:.2f}".format(ans[0].getY()), ")")
circle2PtsRad(p2, p1, 2.0, ans) # reverse p1 with p2
print("The other center is (", "{:.2f}".format(ans[0].getX()), ",", "{:.2f}".format(ans[0].getY()), ")")

main()
Loading

0 comments on commit ac005f9

Please sign in to comment.