## Saturday, 24 December 2016

### Chapter 14 Exercise 11, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

14.11 (Paint a smiley face)
Write a program that paints a smiley face, as shown in
Figure 14.46a.

public class MyPoint {

public double x;
public double y;

public MyPoint(double x, double y) {
this.x = x;
this.y = y;
}

public MyPoint() {
this(0,0);
}

public double x() {
return x;
}

public void setX(double x) {
this.x = x;
}

public double y() {
return y;
}

public void setY(double y) {
this.y = y;
}

public double distance(double x, double y) {
return Math.sqrt((this.x - x) * (this.x - x) + (this.y - y) * (this.y - y));
}

public double distance(MyPoint point) {

return distance(point.x, point.y);
}

public MyPoint getCenterPoint(MyPoint p) {

return new MyPoint((p.x + this.x) / 2, (p.y + this.y) / 2);
}

public static MyPoint getCenterPoint(double x1, double y1, double x2, double y2) {
return new MyPoint((x1 + x2) / 2, (y1 + y2) / 2);
}

/** Return true if this point is on the left side of the
*  directed line from p0 to p1 */
public boolean leftOfTheLine(MyPoint p0, MyPoint p1) {

return leftOfTheLine(p0.x, p0.y, p1.x, p1.y, x, y);
}

/** Return true if this point is on the same
*  line from p0 to p1 */
public boolean onTheSameLine(MyPoint p0, MyPoint p1) {

return onTheSameLine(p0.x, p0.y, p1.x, p1.y, x, y);

}

/** Return true if this point is on the right side of the
*  directed line from p0 to p1 */
public boolean rightOfTheLine(MyPoint p0, MyPoint p1) {

return rightOfTheLine(p0.x, p0.y, p1.x, p1.y, x, y);

}

/** Return true if this point is on the
*  line segment from p0 to p1 */
public boolean onTheLineSegment(MyPoint p0, MyPoint p1) {

return onTheLineSegment(p0.x, p0.y, p1.x, p1.y, x, y);

}

/** Return true if point (x2, y2) is on the left side of the
*  directed line from (x0, y0) to (x1, y1) */
public static boolean leftOfTheLine(double x0, double y0, double x1, double y1, double x2, double y2){

return (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0) > 0;
}
/** Return true if point (x2, y2) is on the same
*  line from (x0, y0) to (x1, y1) */
public static boolean onTheSameLine(double x0, double y0, double x1, double y1, double x2, double y2) {

return (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0) == 0;
}
/** Return true if point (x2, y2) is on the
*  line segment from (x0, y0) to (x1, y1) */
public static boolean onTheLineSegment(double x0, double y0, double x1, double y1, double x2, double y2) {

double position = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);

return position <= 0.0000000001 && ((x0 <= x2 && x2 <= x1) || (x0 >= x2 && x2 >= x1));
}

/** Return true if point (x2, y2) is on the right side of the
*  directed line from (x0, y0) to (x1, y1) */
public static boolean rightOfTheLine(double x0, double y0, double x1, double y1, double x2, double y2){

return (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0) < 0;
}

@Override
public String toString() {
return "(" + x + ", " + y + ")";
}

}


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.Collections;
public class Exercise_11 extends Application {

private static final int CENTER_Y = 200;
private static final int CENTER_X = 200;
private static final int RADIUS = 100;

@Override
public void start(Stage primaryStage) {

Pane pane = new Pane();
ArrayList<Shape> shapes = new ArrayList<>();

// Outer circle of the smiley face
Circle c = new Circle(CENTER_X, CENTER_Y, RADIUS, Color.TRANSPARENT);
c.setStroke(Color.BLACK);

// drawing the nose
Line[] nose = createFixedNose(c);

// drawing left and right eye
double y =  CENTER_Y - (RADIUS / 2.8);
double x = CENTER_X - (RADIUS / 2.8);
for (int i = 0; i < 2; i++) {
Ellipse outerC = new Ellipse(x, y, RADIUS / 4, RADIUS / 6);
outerC.setFill(Color.TRANSPARENT);
outerC.setStroke(Color.BLACK);

Circle pupil = new Circle(outerC.getCenterX(), outerC.getCenterY(), outerC.getRadiusY() / 1.2);
x += (RADIUS / 2.8) * 2;
}

// drawing the face's smile
Arc smile = new Arc(
c.getCenterX(), // center x
c.getCenterY() + (c.getRadius() / 3), // center y
180, 180); // start point and length
smile.setFill(Color.TRANSPARENT);
smile.setStroke(Color.BLACK);

Scene scene = new Scene(pane, CENTER_X + RADIUS * 2, CENTER_Y + RADIUS * 2);
primaryStage.setScene(scene);
primaryStage.setTitle("Smiley Face!");
primaryStage.show();
}

/**
* returns 3 Lines that shape a fixed triangle based
* from the circle's center point and radius
* **/
private Line[] createFixedNose(Circle c) {
MyPoint p1 = new MyPoint(c.getCenterX() - (c.getRadius() / 4), c.getCenterY() + (c.getRadius() / 4));
MyPoint p2 = new MyPoint(c.getCenterX() + (c.getRadius() / 4), c.getCenterY() + (c.getRadius() / 4));
MyPoint p3 = new MyPoint(c.getCenterX(), c.getCenterY() - (c.getRadius() / 4));

Line[] lines = new Line[3];
lines[0] = new Line(p1.x, p1.y, p2.x, p2.y);
lines[1] = new Line(p2.x, p2.y, p3.x, p3.y);
lines[2] = new Line(p3.x, p3.y, p1.x, p1.y);

return lines;
}

public static void main(String[] args) {
Application.launch(args);
}
}