Created
June 6, 2020 05:11
-
-
Save alxhub/814bc886976993912859d5a99a5d2d51 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
copyFlagToPicture( | |
Matrix4 transform, Size deviceSize, img.Image flag, img.Image pic) async { | |
print("device: " + deviceSize.toString()); | |
var d_flag_scale = deviceSize.width / flag.width; | |
var d_flag_height = flag.height * d_flag_scale; | |
print("d_flag_height: " + d_flag_height.toString()); | |
var d_flag_top = deviceSize.height / 2.0 - (d_flag_height / 2.0); | |
var d_flag_bottom = deviceSize.height / 2.0 + (d_flag_height / 2.0); | |
Vector4 vd_flag_lt = transform * (new Vector4(0.0, d_flag_top, 0.0, 1.0)); | |
Vector4 vd_flag_rt = | |
transform * (new Vector4(deviceSize.width, d_flag_top, 0.0, 1.0)); | |
Vector4 vd_flag_lb = transform * (new Vector4(0.0, d_flag_bottom, 0.0, 1.0)); | |
Vector4 vd_flag_rb = | |
transform * (new Vector4(deviceSize.width, d_flag_bottom, 0.0, 1.0)); | |
print("LeftTop: " + vd_flag_lt.toString()); | |
print("RightTop: " + vd_flag_rt.toString()); | |
print("LeftBottom: " + vd_flag_lb.toString()); | |
print("RightBottom: " + vd_flag_rb.toString()); | |
// Compute the rotation angle and rotate the flag. | |
var vd_flag_orient = (vd_flag_rt - vd_flag_lt).normalized(); | |
print("Orientation vector: " + vd_flag_orient.toString()); | |
var angle = atan2(vd_flag_orient.y, vd_flag_orient.x) * 180 / pi; | |
print("Rotational angle: " + angle.toString()); | |
var flagRotated = img.copyRotate(flag, angle); | |
print("FlagRotated: " + | |
flagRotated.width.toString() + | |
"x" + | |
flagRotated.height.toString()); | |
// Find the flag bounding box. | |
var d_flag_min_x = | |
min(min(vd_flag_lt.x, vd_flag_rt.x), min(vd_flag_lb.x, vd_flag_rb.x)); | |
var d_flag_min_y = | |
min(min(vd_flag_lt.y, vd_flag_rt.y), min(vd_flag_lb.y, vd_flag_rb.y)); | |
var d_flag_max_x = | |
max(max(vd_flag_lt.x, vd_flag_rt.x), max(vd_flag_lb.x, vd_flag_rb.x)); | |
var d_flag_max_y = | |
max(max(vd_flag_lt.y, vd_flag_rt.y), max(vd_flag_lb.y, vd_flag_rb.y)); | |
print("bounding box:"); | |
print(" x: " + d_flag_min_x.toString() + " - " + d_flag_max_x.toString()); | |
print(" y: " + d_flag_min_y.toString() + " - " + d_flag_max_y.toString()); | |
var d_flag_w = d_flag_max_x - d_flag_min_x; | |
var d_flag_h = d_flag_max_y - d_flag_min_y; | |
print(" w: " + d_flag_w.toString() + ", h: " + d_flag_h.toString()); | |
// Determine relative bounds on the rotated flag image to copy to the picture. | |
pic = img.copyRotate(pic, 90); | |
print("rotated pic size: " + | |
pic.width.toString() + | |
"x" + | |
pic.height.toString()); | |
var dp_scale_x = pic.width / deviceSize.width; | |
var dp_scale_y = pic.height / deviceSize.height; | |
print("device scale: x: " + | |
dp_scale_x.toString() + | |
", y: " + | |
dp_scale_y.toString()); | |
var p_flag_min_x = d_flag_min_x * dp_scale_x; | |
var p_flag_max_x = d_flag_max_x * dp_scale_x; | |
var p_flag_min_y = d_flag_min_y * dp_scale_y; | |
var p_flag_max_y = d_flag_max_y * dp_scale_y; | |
var p_flag_w = (p_flag_max_x - p_flag_min_x).toInt(); | |
var p_flag_h = (p_flag_max_y - p_flag_min_y).toInt(); | |
var flagResized = | |
img.copyResize(flagRotated, width: p_flag_w, height: p_flag_h); | |
print("resized flag size: " + | |
flagResized.width.toString() + | |
"x" + | |
flagResized.height.toString()); | |
print("copy flag source:"); | |
print( | |
"dst: x: " + p_flag_min_x.toString() + ", y: " + p_flag_min_y.toString()); | |
img.copyInto( | |
pic, | |
flagResized, | |
dstX: p_flag_min_x.toInt(), | |
dstY: p_flag_min_y.toInt(), | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment